Skip to content

Commit

Permalink
allow the none-downcast works through the reload subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed Sep 23, 2022
1 parent 51afb35 commit 1eaa072
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tracing-subscriber/src/reload.rs
Expand Up @@ -23,6 +23,7 @@
use crate::subscribe;
use crate::sync::RwLock;

use core::{any::TypeId, ptr::NonNull};
use std::{
error, fmt,
sync::{Arc, Weak},
Expand Down Expand Up @@ -144,6 +145,19 @@ where
fn max_level_hint(&self) -> Option<LevelFilter> {
try_lock!(self.inner.read(), else return None).max_level_hint()
}

#[doc(hidden)]
unsafe fn downcast_raw(&self, id: TypeId) -> Option<NonNull<()>> {
// It is generally unsafe to downcast through a reload, because
// the pointer can be invalidated after the lock is dropped.
// `NoneLayerMarker` is a special case because it
// is never dereferenced.
if id == TypeId::of::<subscribe::NoneLayerMarker>() {
return try_lock!(self.inner.read(), else return None).downcast_raw(id);
}

None
}
}

#[cfg(all(feature = "registry", feature = "std"))]
Expand Down
22 changes: 22 additions & 0 deletions tracing-subscriber/tests/option.rs
Expand Up @@ -96,3 +96,25 @@ fn doesnt_override_none() {
.with(None::<LevelFilter>);
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

/// lalal
#[test]
fn reload_works_with_none() {
let (subscriber1, handle1) = tracing_subscriber::reload::Subscriber::new(None::<BasicLayer>);
let (subscriber2, _handle2) = tracing_subscriber::reload::Subscriber::new(None::<BasicLayer>);

let subscriber = tracing_subscriber::registry()
.with(subscriber1)
.with(subscriber2);
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));

// reloading one should pass through correctly.
handle1.reload(Some(BasicLayer(None))).unwrap();
assert_eq!(subscriber.max_level_hint(), None);

// Check pass-through of an actual level as well
handle1
.reload(Some(BasicLayer(Some(LevelFilter::DEBUG))))
.unwrap();
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
}

0 comments on commit 1eaa072

Please sign in to comment.