From c2f9ceef926409373382c8689792213f8909ecd5 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Mon, 13 Sep 2021 17:08:19 -0400 Subject: [PATCH] subscriber: add Layer implementations for various containers (#1536) Add `Layer` impls for: - `Box` - `Box>` - `Arc` - `Arc>` where `L: Layer` --- tracing-subscriber/src/subscribe.rs | 97 ++++++++++++++++++++++++++++- 1 file changed, 96 insertions(+), 1 deletion(-) diff --git a/tracing-subscriber/src/subscribe.rs b/tracing-subscriber/src/subscribe.rs index d2e0ce72de..6e4bb21dc0 100644 --- a/tracing-subscriber/src/subscribe.rs +++ b/tracing-subscriber/src/subscribe.rs @@ -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. /// @@ -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 { + 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> { + self.deref().downcast_raw(id) + } + }; +} + +impl Subscribe for Arc +where + S: Subscribe, + C: Collect, +{ + subscriber_impl_body! {} +} + +impl Subscribe for Arc> +where + C: Collect, +{ + subscriber_impl_body! {} +} + +impl Subscribe for Box +where + S: Subscribe, + C: Collect, +{ + subscriber_impl_body! {} +} + +impl Subscribe for Box> +where + C: Collect, +{ + subscriber_impl_body! {} +} + #[cfg(feature = "registry")] #[cfg_attr(docsrs, doc(cfg(feature = "registry")))] impl<'a, S, C> LookupSpan<'a> for Layered