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 <[email protected]>
This commit is contained in:
Eliza Weisman
2019-03-07 15:19:26 -08:00
committed by GitHub
parent 7f911b6b70
commit b01e71b3d8
7 changed files with 23 additions and 21 deletions
+1 -1
View File
@@ -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,
+2 -2
View File
@@ -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))
}
}};
}
+1 -1
View File
@@ -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))
});
-1
View File
@@ -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!(
@@ -52,11 +52,10 @@ pub fn with_default<T>(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<T, F>(mut f: F) -> T
pub fn get_default<T, F>(mut f: F) -> T
where
F: FnMut(&Dispatch) -> T,
{
+2 -2
View File
@@ -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);
});
}
+16 -12
View File
@@ -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: fmt::Display>(T);
#[derive(Debug, Clone)]
pub struct DebugValue<T: fmt::Debug>(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: T) -> DisplayValue<T>
@@ -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] {}
)+
}