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<Option<Id>>` and `impl Into<Option<&'a Id>>`. While `AsRef`
might be more semantically correct for the borrowed-`Id` conversion, its
signature doesn't permit conversion into an `Option`. Implementations of
`Into<Option<Id>>` and `Into<Option<&'a Id>>` 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<Id>` to the span
macros all still compile after this change.

Closes #1143

Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
Eliza Weisman
2019-06-13 12:53:08 -07:00
committed by GitHub
parent 4f6395b31c
commit d4adeeef2f
2 changed files with 31 additions and 63 deletions
+25 -63
View File
@@ -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<I>(
parent: I,
pub fn child_of(
parent: impl Into<Option<Id>>,
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<I>(&self, from: I) -> &Self
where
I: AsId,
{
pub fn follows_from(&self, from: impl for<'a> Into<Option<&'a Id>>) -> &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<Option<&'a Id>> for &'a Span {
fn into(self) -> Option<&'a Id> {
self.inner.as_ref().map(|inner| &inner.id)
}
}
impl<'a> Into<Option<Id>> for &'a Span {
fn into(self) -> Option<Id> {
self.inner.as_ref().map(Inner::id)
}
}
impl Into<Option<Id>> for Span {
fn into(self) -> Option<Id> {
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<Id> {}
impl AsId for Option<Id> {
fn as_id(&self) -> Option<&Id> {
self.as_ref()
}
}
impl<'a> ::sealed::Sealed for &'a Option<Id> {}
impl<'a> AsId for &'a Option<Id> {
fn as_id(&self) -> Option<&Id> {
self.as_ref()
}
}
#[cfg(test)]
mod test {
use super::*;
+6
View File
@@ -60,6 +60,12 @@ impl Id {
}
}
impl<'a> Into<Option<Id>> for &'a Id {
fn into(self) -> Option<Id> {
Some(self.clone())
}
}
// ===== impl Attributes =====
impl<'a> Attributes<'a> {