Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conversion between span::Id and NonZeroU64 #770

Merged
merged 1 commit into from
Jun 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not convinced we need inline attributes for these --- I can't imagine them ever not being inlined. Not a blocker.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the lack of #[inline] in this crate. In my experience, not having the attribute results in a function call, which is needless overhead. However, I haven't tested whether this is the case for newer compilers. I remember seeing a compiler issue tracking this behavior.

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