Skip to content

Commit

Permalink
core: Add conversion between span::Id and NonZeroU64 (#770)
Browse files Browse the repository at this point in the history
## Motivation

Consumers of the crate should be able to handle the 0 case separately
and avoiding panicking within `Id::from_u64`.

## Solution

Expose direct conversion between the inner type.

This allows handling the 0 case separately and avoiding panicking within
`Id::from_u64`.
  • Loading branch information
nvzqz committed Jun 27, 2020
1 parent 7bc225a commit 8628f89
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tracing-core/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,28 @@ impl Id {
Id(NonZeroU64::new(u).expect("span IDs must be > 0"))
}

/// Constructs a new span ID from the given `NonZeroU64`.
///
/// Unlike [`Id::from_u64`](#method.from_u64), this will never panic.
#[inline]
pub const fn from_non_zero_u64(id: NonZeroU64) -> Self {
Id(id)
}

// Allow `into` by-ref since we don't want to impl Copy for Id
#[allow(clippy::wrong_self_convention)]
/// Returns the span's ID as a `u64`.
pub fn into_u64(&self) -> u64 {
self.0.get()
}

// Allow `into` by-ref since we don't want to impl Copy for Id
#[allow(clippy::wrong_self_convention)]
/// Returns the span's ID as a `NonZeroU64`.
#[inline]
pub const fn into_non_zero_u64(&self) -> NonZeroU64 {
self.0
}
}

impl<'a> Into<Option<Id>> for &'a Id {
Expand Down

0 comments on commit 8628f89

Please sign in to comment.