Skip to content

Commit

Permalink
tracing: impl From<EnteredSpan> for Option<Id> (#1325)
Browse files Browse the repository at this point in the history
## Motivation

<!--
Explain the context and why you're making that change. What is the problem
you're trying to solve? If a new feature is being added, describe the intended
use case that feature fulfills.
-->

The following code compiles failed:
```rust
use tracing;

fn main() {

    let span = tracing::info_span!("my_span").entered();
    tracing::info!(parent: &span, "test span");
}
```

```log
   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `Option<Id>: From<&EnteredSpan>` is not satisfied
  --> src/main.rs:6:5
   |
6  |     tracing::info!(parent: &span, "test span");
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&EnteredSpan>` is not implemented for `Option<Id>`
```

## Solution

- `impl<'a> From<&'a EnteredSpan> for Option<&'a Id>`.
- `impl<'a> From<&'a EnteredSpan> for Option<Id>`
- Add `id()` method into `EnteredSpan`.

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
Folyd and hawkw committed Apr 30, 2021
1 parent ccfa2cc commit 88b176c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tracing/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,18 @@ impl From<Span> for Option<Id> {
}
}

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<Id> {
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 {
Expand Down Expand Up @@ -1403,6 +1415,11 @@ impl Clone for Inner {
// ===== impl Entered =====

impl EnteredSpan {
/// Returns this span's `Id`, if it is enabled.
pub fn id(&self) -> Option<Id> {
self.inner.as_ref().map(Inner::id)
}

/// Exits this span, returning the underlying [`Span`].
#[inline]
pub fn exit(mut self) -> Span {
Expand Down Expand Up @@ -1454,6 +1471,7 @@ impl Drop for EnteredSpan {
struct PhantomNotSend {
ghost: PhantomData<*mut ()>,
}

#[allow(non_upper_case_globals)]
const PhantomNotSend: PhantomNotSend = PhantomNotSend { ghost: PhantomData };

Expand Down

0 comments on commit 88b176c

Please sign in to comment.