Skip to content

Commit e93ff47

Browse files
committed
no-std mostly works without alloc now
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
1 parent f4d443d commit e93ff47

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

tracing-core/src/callsite.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,15 @@ mod inner {
205205
/// [`Interest::sometimes()`]: super::subscriber::Interest::sometimes
206206
/// [`Subscriber`]: super::subscriber::Subscriber
207207
pub fn rebuild_interest_cache() {
208-
rebuild_interest(&REGISTRY, crate::dispatcher::get_global());
208+
let dispatcher = crate::dispatcher::get_global();
209+
let mut max_level = LevelFilter::OFF;
210+
// If the subscriber did not provide a max level hint, assume
211+
// that it may enable every level.
212+
let level_hint = dispatcher.max_level_hint().unwrap_or(LevelFilter::TRACE);
213+
214+
REGISTRY.for_each(|reg| rebuild_callsite_interest(dispatcher, reg.callsite));
215+
216+
LevelFilter::set_max(max_level);
209217
}
210218

211219
/// Register a new `Callsite` with the global registry.
@@ -222,17 +230,6 @@ mod inner {
222230

223231
callsite.set_interest(dispatcher.register_callsite(meta))
224232
}
225-
226-
fn rebuild_interest(callsites: &Callsites, dispatcher: &dyn Subscriber) {
227-
let mut max_level = LevelFilter::OFF;
228-
// If the subscriber did not provide a max level hint, assume
229-
// that it may enable every level.
230-
let level_hint = dispatcher.max_level_hint().unwrap_or(LevelFilter::TRACE);
231-
232-
callsites.for_each(|reg| rebuild_callsite_interest(dispatcher, reg.callsite));
233-
234-
LevelFilter::set_max(max_level);
235-
}
236233
}
237234

238235
// ===== impl Identifier =====

tracing-core/src/dispatcher.rs

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,16 @@ const INITIALIZING: usize = 1;
184184
const INITIALIZED: usize = 2;
185185

186186
static mut GLOBAL_DISPATCH: Dispatch = Dispatch {
187+
#[cfg(feature = "std")]
187188
subscriber: Kind::Global(&NO_SUBSCRIBER),
189+
#[cfg(not(feature = "std"))]
190+
subscriber: &NO_SUBSCRIBER,
188191
};
189192
static NONE: Dispatch = Dispatch {
193+
#[cfg(feature = "std")]
190194
subscriber: Kind::Global(&NO_SUBSCRIBER),
195+
#[cfg(not(feature = "std"))]
196+
subscriber: &NO_SUBSCRIBER,
191197
};
192198
static NO_SUBSCRIBER: NoSubscriber = NoSubscriber;
193199

@@ -396,34 +402,34 @@ where
396402
.unwrap_or_else(|_| f(&Dispatch::none()))
397403
}
398404

399-
/// Executes a closure with a reference to this thread's current [dispatcher].
400-
///
401-
/// Note that calls to `get_default` should not be nested; if this function is
402-
/// called while inside of another `get_default`, that closure will be provided
403-
/// with `Dispatch::none` rather than the previously set dispatcher.
404-
///
405-
/// [dispatcher]: super::dispatcher::Dispatch
406-
#[cfg(feature = "std")]
407-
#[doc(hidden)]
408-
#[inline(never)]
409-
pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> {
410-
CURRENT_STATE
411-
.try_with(|state| {
412-
let entered = state.enter()?;
413-
Some(f(&*entered.current()))
414-
})
415-
.ok()?
416-
}
417-
418-
/// Executes a closure with a reference to the current [dispatcher].
419-
///
420-
/// [dispatcher]: super::dispatcher::Dispatch
421-
#[cfg(not(feature = "std"))]
422-
#[doc(hidden)]
423-
pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> {
424-
let dispatch = get_global()?;
425-
Some(f(&dispatch))
426-
}
405+
// /// Executes a closure with a reference to this thread's current [dispatcher].
406+
// ///
407+
// /// Note that calls to `get_default` should not be nested; if this function is
408+
// /// called while inside of another `get_default`, that closure will be provided
409+
// /// with `Dispatch::none` rather than the previously set dispatcher.
410+
// ///
411+
// /// [dispatcher]: super::dispatcher::Dispatch
412+
// #[cfg(feature = "std")]
413+
// #[doc(hidden)]
414+
// #[inline(never)]
415+
// pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> {
416+
// CURRENT_STATE
417+
// .try_with(|state| {
418+
// let entered = state.enter()?;
419+
// Some(f(&*entered.current()))
420+
// })
421+
// .ok()?
422+
// }
423+
424+
// /// Executes a closure with a reference to the current [dispatcher].
425+
// ///
426+
// /// [dispatcher]: super::dispatcher::Dispatch
427+
// #[cfg(not(feature = "std"))]
428+
// #[doc(hidden)]
429+
// pub fn get_current<T>(f: impl FnOnce(&Dispatch) -> T) -> Option<T> {
430+
// let dispatch = get_global()?;
431+
// Some(f(&dispatch))
432+
// }
427433

428434
/// Executes a closure with a reference to the current [dispatcher].
429435
///
@@ -752,6 +758,7 @@ impl fmt::Debug for Dispatch {
752758
}
753759
}
754760

761+
#[cfg(feature = "std")]
755762
impl<S> From<S> for Dispatch
756763
where
757764
S: Subscriber + Send + Sync + 'static,
@@ -788,6 +795,7 @@ impl Subscriber for NoSubscriber {
788795
fn exit(&self, _span: &span::Id) {}
789796
}
790797

798+
#[cfg(feature = "std")]
791799
impl Registrar {
792800
pub(crate) fn upgrade(&self) -> Option<Dispatch> {
793801
match self.0 {
@@ -889,13 +897,13 @@ mod test {
889897

890898
#[test]
891899
fn dispatch_is() {
892-
let dispatcher = Dispatch::new(NoSubscriber);
900+
let dispatcher = Dispatch::from_static(&NO_SUBSCRIBER);
893901
assert!(dispatcher.is::<NoSubscriber>());
894902
}
895903

896904
#[test]
897905
fn dispatch_downcasts() {
898-
let dispatcher = Dispatch::new(NoSubscriber);
906+
let dispatcher = Dispatch::from_static(&NO_SUBSCRIBER);
899907
assert!(dispatcher.downcast_ref::<NoSubscriber>().is_some());
900908
}
901909

tracing-core/src/field.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,6 @@ impl_valid_len! {
828828
mod test {
829829
use super::*;
830830
use crate::metadata::{Kind, Level, Metadata};
831-
use alloc::{borrow::ToOwned, string::String};
832831

833832
struct TestCallsite1;
834833
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1;
@@ -957,6 +956,7 @@ mod test {
957956
}
958957

959958
#[test]
959+
#[cfg(feature = "std")]
960960
fn record_debug_fn() {
961961
let fields = TEST_META_1.fields();
962962
let values = &[
@@ -970,6 +970,6 @@ mod test {
970970
use core::fmt::Write;
971971
write!(&mut result, "{:?}", value).unwrap();
972972
});
973-
assert_eq!(result, "123".to_owned());
973+
assert_eq!(result, String::from("123"));
974974
}
975975
}

tracing-core/tests/global_dispatch.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use common::*;
44
use tracing_core::dispatcher::*;
55
#[test]
66
fn global_dispatch() {
7-
set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed");
7+
static TEST: TestSubscriberA = TestSubscriberA;
8+
set_global_default(Dispatch::from_static(&TEST)).expect("global dispatch set failed");
89
get_default(|current| {
910
assert!(
1011
current.is::<TestSubscriberA>(),
@@ -29,6 +30,6 @@ fn global_dispatch() {
2930
)
3031
});
3132

32-
set_global_default(Dispatch::new(TestSubscriberA))
33+
set_global_default(Dispatch::from_static(&TEST))
3334
.expect_err("double global dispatch set succeeded");
3435
}

0 commit comments

Comments
 (0)