Skip to content

Commit

Permalink
subscriber: add Layer implementations for various containers (#1536)
Browse files Browse the repository at this point in the history
Add `Layer<S>` impls for:
- `Box<L>`
- `Box<dyn Layer<S>>`
- `Arc<L>`
- `Arc<dyn Layer<S>>`
 where `L: Layer<S>`
  • Loading branch information
davidbarsky committed Sep 13, 2021
1 parent 5fdbcbf commit f6ec3d4
Showing 1 changed file with 96 additions and 1 deletion.
97 changes: 96 additions & 1 deletion tracing-subscriber/src/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing_core::{

#[cfg(feature = "registry")]
use crate::registry::{self, LookupSpan, Registry, SpanRef};
use std::{any::TypeId, marker::PhantomData, ptr::NonNull};
use std::{any::TypeId, marker::PhantomData, ops::Deref, ptr::NonNull, sync::Arc};

/// A composable handler for `tracing` events.
///
Expand Down Expand Up @@ -884,6 +884,101 @@ where
}
}

macro_rules! subscriber_impl_body {
() => {
#[inline]
fn new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, C>) {
self.deref().new_span(attrs, id, ctx)
}

#[inline]
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
self.deref().register_callsite(metadata)
}

#[inline]
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, C>) -> bool {
self.deref().enabled(metadata, ctx)
}

#[inline]
fn max_level_hint(&self) -> Option<LevelFilter> {
self.deref().max_level_hint()
}

#[inline]
fn on_record(&self, span: &span::Id, values: &span::Record<'_>, ctx: Context<'_, C>) {
self.deref().on_record(span, values, ctx)
}

#[inline]
fn on_follows_from(&self, span: &span::Id, follows: &span::Id, ctx: Context<'_, C>) {
self.deref().on_follows_from(span, follows, ctx)
}

#[inline]
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
self.deref().on_event(event, ctx)
}

#[inline]
fn on_enter(&self, id: &span::Id, ctx: Context<'_, C>) {
self.deref().on_enter(id, ctx)
}

#[inline]
fn on_exit(&self, id: &span::Id, ctx: Context<'_, C>) {
self.deref().on_exit(id, ctx)
}

#[inline]
fn on_close(&self, id: span::Id, ctx: Context<'_, C>) {
self.deref().on_close(id, ctx)
}

#[inline]
fn on_id_change(&self, old: &span::Id, new: &span::Id, ctx: Context<'_, C>) {
self.deref().on_id_change(old, new, ctx)
}

#[doc(hidden)]
#[inline]
unsafe fn downcast_raw(&self, id: TypeId) -> std::option::Option<NonNull<()>> {
self.deref().downcast_raw(id)
}
};
}

impl<S, C> Subscribe<C> for Arc<S>
where
S: Subscribe<C>,
C: Collect,
{
subscriber_impl_body! {}
}

impl<C> Subscribe<C> for Arc<dyn Subscribe<C>>
where
C: Collect,
{
subscriber_impl_body! {}
}

impl<S, C> Subscribe<C> for Box<S>
where
S: Subscribe<C>,
C: Collect,
{
subscriber_impl_body! {}
}

impl<C> Subscribe<C> for Box<dyn Subscribe<C>>
where
C: Collect,
{
subscriber_impl_body! {}
}

#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
impl<'a, S, C> LookupSpan<'a> for Layered<S, C>
Expand Down

0 comments on commit f6ec3d4

Please sign in to comment.