Skip to content

Commit

Permalink
subscriber: add Layer implementations for various containers (tokio-r…
Browse files Browse the repository at this point in the history
…s#1536)

Add `Layer<S>` impls for:
- `Box<L>`
- `Box<dyn Layer<S>>`
- `Arc<L>`
- `Arc<dyn Layer<S>>`

where `L: Layer<S>`
  • Loading branch information
jsgf authored and kaffarell committed May 22, 2024
1 parent c426287 commit 84379a0
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions tracing-subscriber/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use std::{
any::{type_name, TypeId},
fmt,
marker::PhantomData,
ops::Deref,
sync::Arc,
};

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

macro_rules! layer_impl_body {
() => {
#[inline]
fn new_span(&self, attrs: &span::Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
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<'_, S>) -> 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<'_, S>) {
self.deref().on_record(span, values, ctx)
}

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

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

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

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

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

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

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

impl<L, S> Layer<S> for Arc<L>
where
L: Layer<S>,
S: Subscriber,
{
layer_impl_body! {}
}

impl<S> Layer<S> for Arc<dyn Layer<S>>
where
S: Subscriber,
{
layer_impl_body! {}
}

impl<L, S> Layer<S> for Box<L>
where
L: Layer<S>,
S: Subscriber,
{
layer_impl_body! {}
}

impl<S> Layer<S> for Box<dyn Layer<S>>
where
S: Subscriber,
{
layer_impl_body! {}
}

impl<'a, L, S> LookupSpan<'a> for Layered<L, S>
where
S: Subscriber + LookupSpan<'a>,
Expand Down

0 comments on commit 84379a0

Please sign in to comment.