From 5510ba6dbae3de69d7f69d3a3f1b4bc1e1e179e0 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Mon, 11 Mar 2019 16:18:40 -0700 Subject: [PATCH] trace-core: Require span IDs to be > 0 (#973) This branch changes `tokio_trace_core::span::Id::from_u64` to assert that the integer from which the span ID is constructed is greater than zero. This is to enable future use of non-zero optimization. Unfortunately, we can't actually use a `NonZeroU64` _now_, as that type was only stabilized in Rust 1.28.0, and `tokio`'s current minimum supported Rust version is 1.26.0. Adding and documenting the assertion now allows us to change the internal representation to `NonZeroU64` later (when 1.28.0 is the minimum supported Rust version), without causing a breaking change. Signed-off-by: Eliza Weisman --- tokio-trace/tests/support/subscriber.rs | 2 +- tokio-trace/tokio-trace-core/src/span.rs | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/tokio-trace/tests/support/subscriber.rs b/tokio-trace/tests/support/subscriber.rs index c8f58da1c..842640aad 100644 --- a/tokio-trace/tests/support/subscriber.rs +++ b/tokio-trace/tests/support/subscriber.rs @@ -128,7 +128,7 @@ where spans: Mutex::new(HashMap::new()), expected, current: Mutex::new(Vec::new()), - ids: AtomicUsize::new(0), + ids: AtomicUsize::new(1), filter: self.filter, }; (subscriber, handle) diff --git a/tokio-trace/tokio-trace-core/src/span.rs b/tokio-trace/tokio-trace-core/src/span.rs index 4ec1f0052..ae0452b8f 100644 --- a/tokio-trace/tokio-trace-core/src/span.rs +++ b/tokio-trace/tokio-trace-core/src/span.rs @@ -6,12 +6,13 @@ use {field, Metadata}; /// /// They are generated by [`Subscriber`]s for each span as it is created, by /// the [`new_span`] trait method. See the documentation for that method for -/// more information on span -/// ID generation. +/// more information on span ID generation. /// /// [`Subscriber`]: ../subscriber/trait.Subscriber.html /// [`new_span`]: ../subscriber/trait.Subscriber.html#method.new_span #[derive(Clone, Debug, PartialEq, Eq, Hash)] +// TODO(eliza): when Tokio's minimum Rust version is >= 1.28, change the +// internal representation to a `NonZeroU64`. pub struct Id(u64); /// Attributes provided to a `Subscriber` describing a new span when it is @@ -43,7 +44,13 @@ enum Parent { impl Id { /// Constructs a new span ID from the given `u64`. + /// + /// **Note**: Span IDs must be greater than zero. + /// + /// # Panics + /// - If the provided `u64` is 0 pub fn from_u64(u: u64) -> Self { + assert!(u > 0); Id(u) }