Skip to content

Commit

Permalink
Consistently pass a ref to a Dispatch
Browse files Browse the repository at this point in the history
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 tokio-rs#455
Part of tokio-rs#922
  • Loading branch information
dvdplm committed Oct 16, 2020
1 parent a8cc977 commit 24ecaa2
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions tracing-core/src/dispatcher.rs
Expand Up @@ -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!");
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tracing-core/tests/dispatch.rs
Expand Up @@ -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::<TestSubscriberA>(),
Expand Down
4 changes: 2 additions & 2 deletions tracing-core/tests/global_dispatch.rs
Expand Up @@ -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::<TestSubscriberA>(),
Expand All @@ -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");
}
2 changes: 1 addition & 1 deletion tracing-subscriber/src/fmt/mod.rs
Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion tracing-subscriber/src/util.rs
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/dispatcher.rs
Expand Up @@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion tracing/src/subscriber.rs
Expand Up @@ -40,7 +40,7 @@ pub fn set_global_default<S>(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
Expand Down

0 comments on commit 24ecaa2

Please sign in to comment.