Skip to content

Commit cfd8236

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

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
@@ -206,7 +206,15 @@ mod inner {
206206
/// [`Interest::sometimes()`]: super::subscriber::Interest::sometimes
207207
/// [`Subscriber`]: super::subscriber::Subscriber
208208
pub fn rebuild_interest_cache() {
209-
rebuild_interest(&REGISTRY, crate::dispatcher::get_global());
209+
let dispatcher = crate::dispatcher::get_global();
210+
let mut max_level = LevelFilter::OFF;
211+
// If the subscriber did not provide a max level hint, assume
212+
// that it may enable every level.
213+
let level_hint = dispatcher.max_level_hint().unwrap_or(LevelFilter::TRACE);
214+
215+
REGISTRY.for_each(|reg| rebuild_callsite_interest(dispatcher, reg.callsite));
216+
217+
LevelFilter::set_max(max_level);
210218
}
211219

212220
/// Register a new `Callsite` with the global registry.
@@ -223,17 +231,6 @@ mod inner {
223231

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

239236
// ===== impl Identifier =====

tracing-core/src/dispatcher.rs

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

185185
static mut GLOBAL_DISPATCH: Dispatch = Dispatch {
186+
#[cfg(feature = "std")]
186187
subscriber: Kind::Global(&NO_SUBSCRIBER),
188+
#[cfg(not(feature = "std"))]
189+
subscriber: &NO_SUBSCRIBER,
187190
};
188191
static NONE: Dispatch = Dispatch {
192+
#[cfg(feature = "std")]
189193
subscriber: Kind::Global(&NO_SUBSCRIBER),
194+
#[cfg(not(feature = "std"))]
195+
subscriber: &NO_SUBSCRIBER,
190196
};
191197
static NO_SUBSCRIBER: NoSubscriber = NoSubscriber;
192198

@@ -395,34 +401,34 @@ where
395401
.unwrap_or_else(|_| f(&Dispatch::none()))
396402
}
397403

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

427433
/// Executes a closure with a reference to the current [dispatcher].
428434
///
@@ -751,6 +757,7 @@ impl fmt::Debug for Dispatch {
751757
}
752758
}
753759

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

797+
#[cfg(feature = "std")]
790798
impl Registrar {
791799
pub(crate) fn upgrade(&self) -> Option<Dispatch> {
792800
match self.0 {
@@ -890,13 +898,13 @@ mod test {
890898

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

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

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 crate::stdlib::{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 crate::stdlib::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)