Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tracing-core,tracing-subscriber: more downcast_ref & is methods #2160

Merged
merged 2 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tracing-core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,54 @@ impl dyn Collect {
}
}

impl dyn Collect + Send {
/// Returns `true` if this `Collector` is the same type as `T`.
pub fn is<T: Any>(&self) -> bool {
self.downcast_ref::<T>().is_some()
}

/// Returns some reference to this `Collector` value if it is of type `T`,
/// or `None` if it isn't.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
unsafe {
let raw = self.downcast_raw(TypeId::of::<T>())?;
Some(&*(raw.cast().as_ptr()))
}
}
}

impl dyn Collect + Sync {
/// Returns `true` if this `Collector` is the same type as `T`.
pub fn is<T: Any>(&self) -> bool {
self.downcast_ref::<T>().is_some()
}

/// Returns some reference to this `Collector` value if it is of type `T`,
/// or `None` if it isn't.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
unsafe {
let raw = self.downcast_raw(TypeId::of::<T>())?;
Some(&*(raw.cast().as_ptr()))
}
}
}

impl dyn Collect + Send + Sync {
/// Returns `true` if this `Collector` is the same type as `T`.
pub fn is<T: Any>(&self) -> bool {
self.downcast_ref::<T>().is_some()
}

/// Returns some reference to this `Collector` value if it is of type `T`,
/// or `None` if it isn't.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
unsafe {
let raw = self.downcast_raw(TypeId::of::<T>())?;
Some(&*(raw.cast().as_ptr()))
}
}
}

/// Indicates a [`Collect`]'s interest in a particular callsite.
///
/// Collectors return an `Interest` from their [`register_callsite`] methods
Expand Down
27 changes: 26 additions & 1 deletion tracing-subscriber/src/subscribe/layered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ use crate::{
};
#[cfg(all(feature = "registry", feature = "std"))]
use crate::{filter::FilterId, registry::Registry};
use core::{any::TypeId, cmp, fmt, marker::PhantomData, ptr::NonNull};
use core::{
any::{Any, TypeId},
cmp, fmt,
marker::PhantomData,
ptr::NonNull,
};

/// A [collector] composed of a [collector] wrapped by one or more
/// [subscriber]s.
Expand Down Expand Up @@ -62,6 +67,26 @@ pub struct Layered<S, I, C = I> {

// === impl Layered ===

impl<S, C> Layered<S, C>
where
S: Subscribe<C>,
C: Collect,
{
/// Returns `true` if this `Collector` is the same type as `T`.
pub fn is<T: Any>(&self) -> bool {
self.downcast_ref::<T>().is_some()
}

/// Returns some reference to this `Collector` value if it is of type `T`,
/// or `None` if it isn't.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
unsafe {
let raw = self.downcast_raw(TypeId::of::<T>())?;
Some(&*(raw.cast().as_ptr()))
}
}
}

impl<S, C> Collect for Layered<S, C>
where
S: Subscribe<C>,
Expand Down