From a0824d398aa2511de28371d30dda9203360a6cf5 Mon Sep 17 00:00:00 2001 From: Jack Wrenn Date: Tue, 26 Jul 2022 18:24:01 -0400 Subject: [PATCH] subscriber: impl `LookupSpan` for `Box` and `Arc` (#2247) These implementations mirror those provided by tracing-core for `Subscriber` on `Box` and `Arc`. --- tracing-subscriber/src/layer/mod.rs | 4 +- tracing-subscriber/src/registry/mod.rs | 65 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/tracing-subscriber/src/layer/mod.rs b/tracing-subscriber/src/layer/mod.rs index 24b8533234..5122559ce5 100644 --- a/tracing-subscriber/src/layer/mod.rs +++ b/tracing-subscriber/src/layer/mod.rs @@ -685,7 +685,7 @@ use core::any::TypeId; feature! { #![feature = "alloc"] - use alloc::boxed::Box; + use alloc::{vec::Vec, boxed::Box}; use core::ops::{Deref, DerefMut}; } @@ -1568,8 +1568,6 @@ where feature! { #![any(feature = "std", feature = "alloc")] - #[cfg(not(feature = "std"))] - use alloc::vec::Vec; macro_rules! layer_impl_body { () => { diff --git a/tracing-subscriber/src/registry/mod.rs b/tracing-subscriber/src/registry/mod.rs index 38af53e8ad..0f9fe76a1a 100644 --- a/tracing-subscriber/src/registry/mod.rs +++ b/tracing-subscriber/src/registry/mod.rs @@ -230,6 +230,11 @@ pub struct Scope<'a, R> { feature! { #![any(feature = "alloc", feature = "std")] + use alloc::{ + boxed::Box, + sync::Arc + }; + #[cfg(not(feature = "smallvec"))] use alloc::vec::{self, Vec}; @@ -251,6 +256,66 @@ feature! { #[cfg(feature = "smallvec")] type SpanRefVecArray<'span, L> = [SpanRef<'span, L>; 16]; + impl<'a, S> LookupSpan<'a> for Arc + where + S: LookupSpan<'a>, + { + type Data = >::Data; + + fn span_data(&'a self, id: &Id) -> Option { + self.as_ref().span_data(id) + } + + fn span(&'a self, id: &Id) -> Option> + where + Self: Sized, + { + self.as_ref().span(id).map( + |SpanRef { + registry: _, + data, + #[cfg(feature = "registry")] + filter, + }| SpanRef { + registry: self, + data, + #[cfg(feature = "registry")] + filter, + }, + ) + } + } + + impl<'a, S> LookupSpan<'a> for Box + where + S: LookupSpan<'a>, + { + type Data = >::Data; + + fn span_data(&'a self, id: &Id) -> Option { + self.as_ref().span_data(id) + } + + fn span(&'a self, id: &Id) -> Option> + where + Self: Sized, + { + self.as_ref().span(id).map( + |SpanRef { + registry: _, + data, + #[cfg(feature = "registry")] + filter, + }| SpanRef { + registry: self, + data, + #[cfg(feature = "registry")] + filter, + }, + ) + } + } + impl<'a, R> Scope<'a, R> where R: LookupSpan<'a>,