From 24ecaa265ea786a06c4851bc0b4ca9dbe490776f Mon Sep 17 00:00:00 2001 From: David Palm Date: Fri, 16 Oct 2020 09:27:31 +0200 Subject: [PATCH] Consistently pass a ref to a Dispatch This changes `set_global_default` to take a reference to its `Dispatch` for a more consistent API at the cost of a one-time clone when initializing the `GLOBAL_DISPATCH` static. Closes #455 Part of #922 --- tracing-core/src/dispatcher.rs | 6 +++--- tracing-core/tests/dispatch.rs | 2 +- tracing-core/tests/global_dispatch.rs | 4 ++-- tracing-subscriber/src/fmt/mod.rs | 2 +- tracing-subscriber/src/util.rs | 2 +- tracing/src/dispatcher.rs | 2 +- tracing/src/subscriber.rs | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/tracing-core/src/dispatcher.rs b/tracing-core/src/dispatcher.rs index 658463584e..f37d83d954 100644 --- a/tracing-core/src/dispatcher.rs +++ b/tracing-core/src/dispatcher.rs @@ -100,7 +100,7 @@ //! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); //! // no default subscriber //! -//! dispatcher::set_global_default(my_dispatch) +//! dispatcher::set_global_default(&my_dispatch) //! // `set_global_default` will return an error if the global default //! // subscriber has already been set. //! .expect("global default was already set!"); @@ -279,11 +279,11 @@ pub fn set_default(dispatcher: &Dispatch) -> DefaultGuard { /// [span]: ../span/index.html /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`Event`]: ../event/struct.Event.html -pub fn set_global_default(dispatcher: Dispatch) -> Result<(), SetGlobalDefaultError> { +pub fn set_global_default(dispatcher: &Dispatch) -> Result<(), SetGlobalDefaultError> { if GLOBAL_INIT.compare_and_swap(UNINITIALIZED, INITIALIZING, Ordering::SeqCst) == UNINITIALIZED { unsafe { - GLOBAL_DISPATCH = Some(dispatcher); + GLOBAL_DISPATCH = Some(dispatcher.clone()); } GLOBAL_INIT.store(INITIALIZED, Ordering::SeqCst); EXISTS.store(true, Ordering::Release); diff --git a/tracing-core/tests/dispatch.rs b/tracing-core/tests/dispatch.rs index 7184fc9f33..4faaeb6910 100644 --- a/tracing-core/tests/dispatch.rs +++ b/tracing-core/tests/dispatch.rs @@ -6,7 +6,7 @@ use tracing_core::dispatcher::*; #[cfg(feature = "std")] #[test] fn set_default_dispatch() { - set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); + set_global_default(&Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); get_default(|current| { assert!( current.is::(), diff --git a/tracing-core/tests/global_dispatch.rs b/tracing-core/tests/global_dispatch.rs index d430ac6182..7def7f76d9 100644 --- a/tracing-core/tests/global_dispatch.rs +++ b/tracing-core/tests/global_dispatch.rs @@ -4,7 +4,7 @@ use common::*; use tracing_core::dispatcher::*; #[test] fn global_dispatch() { - set_global_default(Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); + set_global_default(&Dispatch::new(TestSubscriberA)).expect("global dispatch set failed"); get_default(|current| { assert!( current.is::(), @@ -29,6 +29,6 @@ fn global_dispatch() { ) }); - set_global_default(Dispatch::new(TestSubscriberA)) + set_global_default(&Dispatch::new(TestSubscriberA)) .expect_err("double global dispatch set succeeded"); } diff --git a/tracing-subscriber/src/fmt/mod.rs b/tracing-subscriber/src/fmt/mod.rs index c3f256d69c..08c6245617 100644 --- a/tracing-subscriber/src/fmt/mod.rs +++ b/tracing-subscriber/src/fmt/mod.rs @@ -414,7 +414,7 @@ where #[cfg(feature = "tracing-log")] tracing_log::LogTracer::init().map_err(Box::new)?; - tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new( + tracing_core::dispatcher::set_global_default(&tracing_core::dispatcher::Dispatch::new( self.finish(), ))?; Ok(()) diff --git a/tracing-subscriber/src/util.rs b/tracing-subscriber/src/util.rs index f6d95c3714..9766e60c45 100644 --- a/tracing-subscriber/src/util.rs +++ b/tracing-subscriber/src/util.rs @@ -53,7 +53,7 @@ where #[cfg(feature = "tracing-log")] tracing_log::LogTracer::init().map_err(TryInitError::new)?; - dispatcher::set_global_default(self.into()).map_err(TryInitError::new)?; + dispatcher::set_global_default(&self.into()).map_err(TryInitError::new)?; Ok(()) } diff --git a/tracing/src/dispatcher.rs b/tracing/src/dispatcher.rs index 4041c994fb..93726d6f8c 100644 --- a/tracing/src/dispatcher.rs +++ b/tracing/src/dispatcher.rs @@ -100,7 +100,7 @@ //! # let my_dispatch = dispatcher::Dispatch::new(my_subscriber); //! // no default subscriber //! -//! dispatcher::set_global_default(my_dispatch) +//! dispatcher::set_global_default(&my_dispatch) //! // `set_global_default` will return an error if the global default //! // subscriber has already been set. //! .expect("global default was already set!"); diff --git a/tracing/src/subscriber.rs b/tracing/src/subscriber.rs index 8f35d84e09..fea16ee510 100644 --- a/tracing/src/subscriber.rs +++ b/tracing/src/subscriber.rs @@ -40,7 +40,7 @@ pub fn set_global_default(subscriber: S) -> Result<(), SetGlobalDefaultError> where S: Subscriber + Send + Sync + 'static, { - crate::dispatcher::set_global_default(crate::Dispatch::new(subscriber)) + crate::dispatcher::set_global_default(&crate::Dispatch::new(subscriber)) } /// Sets the subscriber as the default for the duration of the lifetime of the