From b01e71b3d87a6dc6b8aff3fae6de111e2a4bab0f Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Thu, 7 Mar 2019 15:19:26 -0800 Subject: [PATCH] trace-core: API polish (#962) This branch makes a handful of `tokio-trace-core` API improvements, mostly around naming. In particular: * Rename `dispatcher::with` to `dispatcher::get_default` * Rename `Event::observe` to `Event::dispatch` * Make `field::ValidLen` trait private Closes #948 Closes #960 Signed-off-by: Eliza Weisman --- tokio-trace/examples/counters.rs | 2 +- tokio-trace/src/lib.rs | 4 +-- tokio-trace/src/span.rs | 2 +- tokio-trace/tests/support/subscriber.rs | 1 - .../tokio-trace-core/src/dispatcher.rs | 3 +- tokio-trace/tokio-trace-core/src/event.rs | 4 +-- tokio-trace/tokio-trace-core/src/field.rs | 28 +++++++++++-------- 7 files changed, 23 insertions(+), 21 deletions(-) diff --git a/tokio-trace/examples/counters.rs b/tokio-trace/examples/counters.rs index cb8e3a62f..3d120405f 100644 --- a/tokio-trace/examples/counters.rs +++ b/tokio-trace/examples/counters.rs @@ -2,7 +2,7 @@ extern crate tokio_trace; use tokio_trace::{ - field::{self, Field, Visit}, + field::{Field, Visit}, span, subscriber::{self, Subscriber}, Event, Id, Metadata, diff --git a/tokio-trace/src/lib.rs b/tokio-trace/src/lib.rs index ec64dbe67..7bdeeb510 100644 --- a/tokio-trace/src/lib.rs +++ b/tokio-trace/src/lib.rs @@ -740,7 +740,7 @@ macro_rules! event { }; if is_enabled!(callsite) { let meta = callsite.metadata(); - Event::observe(meta, &valueset!(meta.fields(), $( $k = $val),* )); + Event::dispatch(meta, &valueset!(meta.fields(), $( $k = $val),* )); } } }); @@ -1091,7 +1091,7 @@ macro_rules! is_enabled { true } else { let meta = $callsite.metadata(); - $crate::dispatcher::with(|current| current.enabled(meta)) + $crate::dispatcher::get_default(|current| current.enabled(meta)) } }}; } diff --git a/tokio-trace/src/span.rs b/tokio-trace/src/span.rs index b7e426779..7d28e16e4 100644 --- a/tokio-trace/src/span.rs +++ b/tokio-trace/src/span.rs @@ -272,7 +272,7 @@ impl<'a> Span<'a> { #[inline(always)] fn make(meta: &'a Metadata<'a>, new_span: Attributes) -> Span<'a> { - let inner = dispatcher::with(move |dispatch| { + let inner = dispatcher::get_default(move |dispatch| { let id = dispatch.new_span(&new_span); Some(Inner::new(id, dispatch, meta)) }); diff --git a/tokio-trace/tests/support/subscriber.rs b/tokio-trace/tests/support/subscriber.rs index fbd01f647..c8f58da1c 100644 --- a/tokio-trace/tests/support/subscriber.rs +++ b/tokio-trace/tests/support/subscriber.rs @@ -185,7 +185,6 @@ where fn new_span(&self, span: &Attributes) -> Id { use span::Parent; let meta = span.metadata(); - let values = span.values(); let id = self.ids.fetch_add(1, Ordering::SeqCst); let id = Id::from_u64(id as u64); println!( diff --git a/tokio-trace/tokio-trace-core/src/dispatcher.rs b/tokio-trace/tokio-trace-core/src/dispatcher.rs index 6b769d7a3..9c17c48bf 100644 --- a/tokio-trace/tokio-trace-core/src/dispatcher.rs +++ b/tokio-trace/tokio-trace-core/src/dispatcher.rs @@ -52,11 +52,10 @@ pub fn with_default(dispatcher: Dispatch, f: impl FnOnce() -> T) -> T { let _guard = ResetGuard(prior.ok()); f() } - /// Executes a closure with a reference to this thread's current [dispatcher]. /// /// [dispatcher]: ../dispatcher/struct.Dispatch.html -pub fn with(mut f: F) -> T +pub fn get_default(mut f: F) -> T where F: FnMut(&Dispatch) -> T, { diff --git a/tokio-trace/tokio-trace-core/src/event.rs b/tokio-trace/tokio-trace-core/src/event.rs index ff2f194c4..34c78c860 100644 --- a/tokio-trace/tokio-trace-core/src/event.rs +++ b/tokio-trace/tokio-trace-core/src/event.rs @@ -27,9 +27,9 @@ impl<'a> Event<'a> { /// Constructs a new `Event` with the specified metadata and set of values, /// and observes it with the current subscriber. #[inline] - pub fn observe(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet) { + pub fn dispatch(metadata: &'a Metadata<'a>, fields: &'a field::ValueSet) { let event = Event { metadata, fields }; - ::dispatcher::with(|current| { + ::dispatcher::get_default(|current| { current.event(&event); }); } diff --git a/tokio-trace/tokio-trace-core/src/field.rs b/tokio-trace/tokio-trace-core/src/field.rs index a43d825c3..84b01543c 100644 --- a/tokio-trace/tokio-trace-core/src/field.rs +++ b/tokio-trace/tokio-trace-core/src/field.rs @@ -45,6 +45,8 @@ use std::{ ops::Range, }; +use self::private::ValidLen; + /// An opaque key allowing _O_(1) access to a field in a `Span`'s key-value /// data. /// @@ -227,15 +229,6 @@ pub struct DisplayValue(T); #[derive(Debug, Clone)] pub struct DebugValue(T); -/// Marker trait implemented by arrays which are of valid length to -/// construct a `ValueSet`. -/// -/// `ValueSet`s may only be constructed from arrays containing 32 or fewer -/// elements, to ensure the array is small enough to always be allocated on the -/// stack. This trait is only implemented by arrays of an appropriate length, -/// ensuring that the correct size arrays are used at compile-time. -pub trait ValidLen<'a>: ::sealed::Sealed + Borrow<[(&'a Field, Option<&'a (Value + 'a)>)]> {} - /// Wraps a type implementing `fmt::Display` as a `Value` that can be /// recorded using its `Display` implementation. pub fn display(t: T) -> DisplayValue @@ -615,12 +608,23 @@ impl<'a> fmt::Debug for ValueSet<'a> { // ===== impl ValidLen ===== +mod private { + use super::*; + + /// Marker trait implemented by arrays which are of valid length to + /// construct a `ValueSet`. + /// + /// `ValueSet`s may only be constructed from arrays containing 32 or fewer + /// elements, to ensure the array is small enough to always be allocated on the + /// stack. This trait is only implemented by arrays of an appropriate length, + /// ensuring that the correct size arrays are used at compile-time. + pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (Value + 'a)>)]> {} +} + macro_rules! impl_valid_len { ( $( $len:tt ),+ ) => { $( - impl<'a> ::sealed::Sealed for - [(&'a Field, Option<&'a (Value + 'a)>); $len] {} - impl<'a> ValidLen<'a> for + impl<'a> private::ValidLen<'a> for [(&'a Field, Option<&'a (Value + 'a)>); $len] {} )+ }