diff --git a/tracing/src/span.rs b/tracing/src/span.rs index d7bd75d98c..9d381cbf10 100644 --- a/tracing/src/span.rs +++ b/tracing/src/span.rs @@ -1309,6 +1309,18 @@ impl<'a> From<&'a Span> for Option { } } +impl<'a> From<&'a EnteredSpan> for Option<&'a Id> { + fn from(span: &'a EnteredSpan) -> Self { + span.inner.as_ref().map(|inner| &inner.id) + } +} + +impl<'a> From<&'a EnteredSpan> for Option { + fn from(span: &'a EnteredSpan) -> Self { + span.inner.as_ref().map(Inner::id) + } +} + impl Drop for Span { fn drop(&mut self) { if let Some(Inner { @@ -1394,6 +1406,11 @@ impl Clone for Inner { // ===== impl Entered ===== impl EnteredSpan { + /// Returns this span's `Id`, if it is enabled. + pub fn id(&self) -> Option { + self.inner.as_ref().map(Inner::id) + } + /// Exits this span, returning the underlying [`Span`]. #[inline] pub fn exit(mut self) -> Span { @@ -1445,6 +1462,7 @@ impl Drop for EnteredSpan { struct PhantomNotSend { ghost: PhantomData<*mut ()>, } + #[allow(non_upper_case_globals)] const PhantomNotSend: PhantomNotSend = PhantomNotSend { ghost: PhantomData };