diff --git a/tracing-core/src/dispatcher.rs b/tracing-core/src/dispatcher.rs index da3f7b4af1..d02eb0e790 100644 --- a/tracing-core/src/dispatcher.rs +++ b/tracing-core/src/dispatcher.rs @@ -131,7 +131,7 @@ //! [`Dispatch`]: struct.Dispatch.html use crate::{ callsite, span, - subscriber::{self, Subscriber}, + subscriber::{self, NoSubscriber, Subscriber}, Event, LevelFilter, Metadata, }; @@ -404,7 +404,7 @@ impl Dispatch { #[inline] pub fn none() -> Self { Dispatch { - subscriber: Arc::new(NoSubscriber), + subscriber: Arc::new(NoSubscriber::default()), } } @@ -661,32 +661,6 @@ where } } -struct NoSubscriber; -impl Subscriber for NoSubscriber { - #[inline] - fn register_callsite(&self, _: &'static Metadata<'static>) -> subscriber::Interest { - subscriber::Interest::never() - } - - fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { - span::Id::from_u64(0xDEAD) - } - - fn event(&self, _event: &Event<'_>) {} - - fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {} - - fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} - - #[inline] - fn enabled(&self, _metadata: &Metadata<'_>) -> bool { - false - } - - fn enter(&self, _span: &span::Id) {} - fn exit(&self, _span: &span::Id) {} -} - impl Registrar { pub(crate) fn try_register( &self, @@ -789,13 +763,13 @@ mod test { #[test] fn dispatch_is() { - let dispatcher = Dispatch::new(NoSubscriber); + let dispatcher = Dispatch::new(NoSubscriber::default()); assert!(dispatcher.is::()); } #[test] fn dispatch_downcasts() { - let dispatcher = Dispatch::new(NoSubscriber); + let dispatcher = Dispatch::new(NoSubscriber::default()); assert!(dispatcher.downcast_ref::().is_some()); } diff --git a/tracing-core/src/subscriber.rs b/tracing-core/src/subscriber.rs index 0e07263238..6e943808fd 100644 --- a/tracing-core/src/subscriber.rs +++ b/tracing-core/src/subscriber.rs @@ -564,6 +564,44 @@ impl Interest { } } +/// A no-op [`Subscriber`] +/// +/// [`NoSubscriber`] implements the [`Subscriber`] trait by never being enabled, +/// never being interested in any callsite, and drops all spans and events. +#[derive(Debug, Copy, Clone)] +pub struct NoSubscriber(()); + +impl Default for NoSubscriber { + fn default() -> Self { + NoSubscriber(()) + } +} + +impl Subscriber for NoSubscriber { + #[inline] + fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest { + Interest::never() + } + + fn new_span(&self, _: &span::Attributes<'_>) -> span::Id { + span::Id::from_u64(0xDEAD) + } + + fn event(&self, _event: &Event<'_>) {} + + fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {} + + fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {} + + #[inline] + fn enabled(&self, _metadata: &Metadata<'_>) -> bool { + false + } + + fn enter(&self, _span: &span::Id) {} + fn exit(&self, _span: &span::Id) {} +} + impl Subscriber for Box { #[inline] fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest { diff --git a/tracing-subscriber/src/layer/mod.rs b/tracing-subscriber/src/layer/mod.rs index 9a98fc5f43..7290d65847 100644 --- a/tracing-subscriber/src/layer/mod.rs +++ b/tracing-subscriber/src/layer/mod.rs @@ -1150,7 +1150,7 @@ where layer_impl_body! {} } -impl Layer for Arc> +impl Layer for Arc + Send + Sync> where S: Subscriber, { @@ -1165,7 +1165,7 @@ where layer_impl_body! {} } -impl Layer for Box> +impl Layer for Box + Send + Sync> where S: Subscriber, {