From d4adeeef2f30a78b147207738b0fcdc0cc8ac5e7 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 13 Jun 2019 12:53:08 -0700 Subject: [PATCH] trace: Remove the `AsId` trait (#1145) While we're making breaking changes to `tokio-trace`, it would be good to get rid of the `AsId` trait. The goal of span functions that are generic over `Span`/`Id` can be achieved without the unnecessary complexity of defining a new trait. This would also make the API added to `tokio_trace_core::Event` in #1109 more consistent with the `tokio-trace::Span` API. This branch removes `AsId` from `tokio-trace` and replaces its uses with `impl Into>` and `impl Into>`. While `AsRef` might be more semantically correct for the borrowed-`Id` conversion, its signature doesn't permit conversion into an `Option`. Implementations of `Into>` and `Into>` have been added for `tokio_trace::Span`. This is _technically_ a breaking API change, as it changes function signatures. However, the existing macro syntax still works as-is, and the tests which pass `&Id`, `&Span`, and `&Option` to the span macros all still compile after this change. Closes #1143 Signed-off-by: Eliza Weisman --- tokio-trace/src/span.rs | 88 +++++++----------------- tokio-trace/tokio-trace-core/src/span.rs | 6 ++ 2 files changed, 31 insertions(+), 63 deletions(-) diff --git a/tokio-trace/src/span.rs b/tokio-trace/src/span.rs index 4c4cb9f5b..d1a1e64c4 100644 --- a/tokio-trace/src/span.rs +++ b/tokio-trace/src/span.rs @@ -240,16 +240,13 @@ impl Span { /// [metadata]: ../metadata /// [field values]: ../field/struct.ValueSet.html /// [`follows_from`]: ../struct.Span.html#method.follows_from - pub fn child_of( - parent: I, + pub fn child_of( + parent: impl Into>, meta: &'static Metadata<'static>, values: &field::ValueSet, - ) -> Span - where - I: AsId, - { - let new_span = match parent.as_id() { - Some(parent) => Attributes::child_of(parent.clone(), meta, values), + ) -> Span { + let new_span = match parent.into() { + Some(parent) => Attributes::child_of(parent, meta, values), None => Attributes::new_root(meta, values), }; Self::make(meta, new_span) @@ -467,12 +464,9 @@ impl Span { /// /// If this span is disabled, or the resulting follows-from relationship /// would be invalid, this function will do nothing. - pub fn follows_from(&self, from: I) -> &Self - where - I: AsId, - { + pub fn follows_from(&self, from: impl for<'a> Into>) -> &Self { if let Some(ref inner) = self.inner { - if let Some(from) = from.as_id() { + if let Some(from) = from.into() { inner.follows_from(from); } } @@ -561,6 +555,24 @@ impl fmt::Debug for Span { } } +impl<'a> Into> for &'a Span { + fn into(self) -> Option<&'a Id> { + self.inner.as_ref().map(|inner| &inner.id) + } +} + +impl<'a> Into> for &'a Span { + fn into(self) -> Option { + self.inner.as_ref().map(Inner::id) + } +} + +impl Into> for Span { + fn into(self) -> Option { + self.inner.as_ref().map(Inner::id) + } +} + // ===== impl Inner ===== impl Inner { @@ -667,56 +679,6 @@ impl<'a> fmt::Display for FmtAttrs<'a> { } } -// ===== impl AsId ===== - -impl ::sealed::Sealed for Span {} - -impl AsId for Span { - fn as_id(&self) -> Option<&Id> { - self.inner.as_ref().map(|inner| &inner.id) - } -} - -impl<'a> ::sealed::Sealed for &'a Span {} - -impl<'a> AsId for &'a Span { - fn as_id(&self) -> Option<&Id> { - self.inner.as_ref().map(|inner| &inner.id) - } -} - -impl ::sealed::Sealed for Id {} - -impl AsId for Id { - fn as_id(&self) -> Option<&Id> { - Some(self) - } -} - -impl<'a> ::sealed::Sealed for &'a Id {} - -impl<'a> AsId for &'a Id { - fn as_id(&self) -> Option<&Id> { - Some(self) - } -} - -impl ::sealed::Sealed for Option {} - -impl AsId for Option { - fn as_id(&self) -> Option<&Id> { - self.as_ref() - } -} - -impl<'a> ::sealed::Sealed for &'a Option {} - -impl<'a> AsId for &'a Option { - fn as_id(&self) -> Option<&Id> { - self.as_ref() - } -} - #[cfg(test)] mod test { use super::*; diff --git a/tokio-trace/tokio-trace-core/src/span.rs b/tokio-trace/tokio-trace-core/src/span.rs index ae0452b8f..7858e2c0b 100644 --- a/tokio-trace/tokio-trace-core/src/span.rs +++ b/tokio-trace/tokio-trace-core/src/span.rs @@ -60,6 +60,12 @@ impl Id { } } +impl<'a> Into> for &'a Id { + fn into(self) -> Option { + Some(self.clone()) + } +} + // ===== impl Attributes ===== impl<'a> Attributes<'a> {