Skip to content

Commit

Permalink
Add lifetime to field::Visit
Browse files Browse the repository at this point in the history
Partially an exercise in "how bad would it be?" This allows the removal
of the unsafety in tracing-log's LogVisitor (see next commit), as well
as generalization of NormalizeEvent to more RecordField containers.
  • Loading branch information
CAD97 committed Jun 14, 2022
1 parent 758df19 commit 557103e
Show file tree
Hide file tree
Showing 23 changed files with 210 additions and 186 deletions.
2 changes: 1 addition & 1 deletion examples/examples/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct Count<'a> {
counters: RwLockReadGuard<'a, HashMap<String, AtomicUsize>>,
}

impl<'a> Visit for Count<'a> {
impl<'a> Visit<'_> for Count<'a> {
fn record_i64(&mut self, field: &Field, value: i64) {
if let Some(counter) = self.counters.get(field.name()) {
if value > 0 {
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/sloggish/sloggish_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ impl Span {
}
}

impl Visit for Span {
impl Visit<'_> for Span {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
self.kvs.push((field.name(), format!("{:?}", value)))
}
}

impl<'a> Visit for Event<'a> {
impl<'a> Visit<'_> for Event<'a> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
write!(
&mut self.stderr,
Expand Down
2 changes: 1 addition & 1 deletion tracing-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a> Event<'a> {
///
/// [visitor]: super::field::Visit
#[inline]
pub fn record(&self, visitor: &mut dyn field::Visit) {
pub fn record(&self, visitor: &mut dyn field::Visit<'a>) {
self.fields.record(visitor);
}

Expand Down
60 changes: 34 additions & 26 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct Iter {
/// string: &'a mut String,
/// }
///
/// impl<'a> Visit for StringVisitor<'a> {
/// impl<'a> Visit<'_> for StringVisitor<'a> {
/// fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
/// write!(self.string, "{} = {:?}; ", field.name(), value).unwrap();
/// }
Expand Down Expand Up @@ -145,7 +145,7 @@ pub struct Iter {
/// sum: i64,
/// }
///
/// impl Visit for SumVisitor {
/// impl Visit<'_> for SumVisitor {
/// fn record_i64(&mut self, _field: &Field, value: i64) {
/// self.sum += value;
/// }
Expand Down Expand Up @@ -178,7 +178,7 @@ pub struct Iter {
/// [records an `Event`]: super::collect::Collect::event
/// [set of `Value`s added to a `Span`]: super::collect::Collect::record
/// [`Event`]: super::event::Event
pub trait Visit {
pub trait Visit<'a> {
/// Visit a double-precision floating point value.
fn record_f64(&mut self, field: &Field, value: f64) {
self.record_debug(field, &value)
Expand All @@ -200,7 +200,7 @@ pub trait Visit {
}

/// Visit a string value.
fn record_str(&mut self, field: &Field, value: &str) {
fn record_str(&mut self, field: &Field, value: &'a str) {
self.record_debug(field, &value)
}

Expand Down Expand Up @@ -230,7 +230,7 @@ pub trait Visit {
/// [visitor]: Visit
pub trait Value: crate::sealed::Sealed {
/// Visits this value with the given `Visitor`.
fn record(&self, key: &Field, visitor: &mut dyn Visit);
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>);
}

/// A `Value` which serializes using `fmt::Display`.
Expand Down Expand Up @@ -264,19 +264,19 @@ where

// ===== impl Visit =====

impl<'a, 'b> Visit for fmt::DebugStruct<'a, 'b> {
impl<'a, 'b> Visit<'_> for fmt::DebugStruct<'a, 'b> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
self.field(field.name(), value);
}
}

impl<'a, 'b> Visit for fmt::DebugMap<'a, 'b> {
impl<'a, 'b> Visit<'_> for fmt::DebugMap<'a, 'b> {
fn record_debug(&mut self, field: &Field, value: &dyn fmt::Debug) {
self.entry(&format_args!("{}", field), value);
}
}

impl<F> Visit for F
impl<F> Visit<'_> for F
where
F: FnMut(&Field, &dyn fmt::Debug),
{
Expand Down Expand Up @@ -351,7 +351,11 @@ macro_rules! impl_one_value {
(normal, $value_ty:tt, $op:expr, $record:ident) => {
impl $crate::sealed::Sealed for $value_ty {}
impl $crate::field::Value for $value_ty {
fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) {
fn record(
&self,
key: &$crate::field::Field,
visitor: &mut dyn $crate::field::Visit<'_>,
) {
visitor.$record(key, $op(*self))
}
}
Expand All @@ -366,7 +370,11 @@ macro_rules! impl_one_value {
use num::*;
impl $crate::sealed::Sealed for ty_to_nonzero!($value_ty) {}
impl $crate::field::Value for ty_to_nonzero!($value_ty) {
fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) {
fn record(
&self,
key: &$crate::field::Field,
visitor: &mut dyn $crate::field::Visit<'_>,
) {
visitor.$record(key, $op(self.get()))
}
}
Expand Down Expand Up @@ -399,15 +407,15 @@ impl_values! {

impl<T: crate::sealed::Sealed> crate::sealed::Sealed for Wrapping<T> {}
impl<T: crate::field::Value> crate::field::Value for Wrapping<T> {
fn record(&self, key: &crate::field::Field, visitor: &mut dyn crate::field::Visit) {
fn record<'a>(&'a self, key: &crate::field::Field, visitor: &mut dyn crate::field::Visit<'a>) {
self.0.record(key, visitor)
}
}

impl crate::sealed::Sealed for str {}

impl Value for str {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
visitor.record_str(key, self)
}
}
Expand All @@ -418,7 +426,7 @@ impl crate::sealed::Sealed for dyn std::error::Error + 'static {}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Value for dyn std::error::Error + 'static {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
visitor.record_error(key, self)
}
}
Expand All @@ -429,7 +437,7 @@ impl crate::sealed::Sealed for dyn std::error::Error + Send + 'static {}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Value for dyn std::error::Error + Send + 'static {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
(self as &dyn std::error::Error).record(key, visitor)
}
}
Expand All @@ -440,7 +448,7 @@ impl crate::sealed::Sealed for dyn std::error::Error + Sync + 'static {}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Value for dyn std::error::Error + Sync + 'static {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
(self as &dyn std::error::Error).record(key, visitor)
}
}
Expand All @@ -451,7 +459,7 @@ impl crate::sealed::Sealed for dyn std::error::Error + Send + Sync + 'static {}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Value for dyn std::error::Error + Send + Sync + 'static {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
(self as &dyn std::error::Error).record(key, visitor)
}
}
Expand All @@ -462,7 +470,7 @@ impl<'a, T: ?Sized> Value for &'a T
where
T: Value + 'a,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'visit>(&'visit self, key: &Field, visitor: &mut dyn Visit<'visit>) {
(*self).record(key, visitor)
}
}
Expand All @@ -473,7 +481,7 @@ impl<'a, T: ?Sized> Value for &'a mut T
where
T: Value + 'a,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'visit>(&'visit self, key: &Field, visitor: &mut dyn Visit<'visit>) {
// Don't use `(*self).record(key, visitor)`, otherwise would
// cause stack overflow due to `unconditional_recursion`.
T::record(self, key, visitor)
Expand All @@ -483,7 +491,7 @@ where
impl<'a> crate::sealed::Sealed for fmt::Arguments<'a> {}

impl<'a> Value for fmt::Arguments<'a> {
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'visit>(&'visit self, key: &Field, visitor: &mut dyn Visit<'visit>) {
visitor.record_debug(key, self)
}
}
Expand All @@ -498,7 +506,7 @@ where
T: Value,
{
#[inline]
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record<'a>(&'a self, key: &Field, visitor: &mut dyn Visit<'a>) {
self.as_ref().record(key, visitor)
}
}
Expand Down Expand Up @@ -546,7 +554,7 @@ impl<T> Value for DisplayValue<T>
where
T: fmt::Display,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record(&self, key: &Field, visitor: &mut dyn Visit<'_>) {
visitor.record_debug(key, self)
}
}
Expand All @@ -571,7 +579,7 @@ impl<T: fmt::Debug> Value for DebugValue<T>
where
T: fmt::Debug,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
fn record(&self, key: &Field, visitor: &mut dyn Visit<'_>) {
visitor.record_debug(key, &self.0)
}
}
Expand All @@ -585,7 +593,7 @@ impl<T: fmt::Debug> fmt::Debug for DebugValue<T> {
impl crate::sealed::Sealed for Empty {}
impl Value for Empty {
#[inline]
fn record(&self, _: &Field, _: &mut dyn Visit) {}
fn record(&self, _: &Field, _: &mut dyn Visit<'_>) {}
}

// ===== impl Field =====
Expand Down Expand Up @@ -796,7 +804,7 @@ impl<'a> ValueSet<'a> {
/// Visits all the fields in this `ValueSet` with the provided [visitor].
///
/// [visitor]: Visit
pub fn record(&self, visitor: &mut dyn Visit) {
pub fn record(&self, visitor: &mut dyn Visit<'a>) {
let my_callsite = self.callsite();
for (field, value) in self.values {
if field.callsite() != my_callsite {
Expand Down Expand Up @@ -991,7 +999,7 @@ mod test {
];

struct MyVisitor;
impl Visit for MyVisitor {
impl Visit<'_> for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
assert_eq!(field.callsite(), TEST_META_1.callsite())
}
Expand All @@ -1010,7 +1018,7 @@ mod test {
];

struct MyVisitor;
impl Visit for MyVisitor {
impl Visit<'_> for MyVisitor {
fn record_debug(&mut self, field: &Field, _: &dyn (core::fmt::Debug)) {
assert_eq!(field.name(), "bar")
}
Expand Down
4 changes: 2 additions & 2 deletions tracing-core/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<'a> Attributes<'a> {
/// [Visitor].
///
/// [visitor]: super::field::Visit
pub fn record(&self, visitor: &mut dyn field::Visit) {
pub fn record(&self, visitor: &mut dyn field::Visit<'a>) {
self.values.record(visitor)
}

Expand Down Expand Up @@ -222,7 +222,7 @@ impl<'a> Record<'a> {
/// Records all the fields in this `Record` with the provided [Visitor].
///
/// [visitor]: super::field::Visit
pub fn record(&self, visitor: &mut dyn field::Visit) {
pub fn record(&self, visitor: &mut dyn field::Visit<'a>) {
self.values.record(visitor)
}

Expand Down
4 changes: 2 additions & 2 deletions tracing-error/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) struct WithContext(
impl<C, F> Subscribe<C> for ErrorSubscriber<C, F>
where
C: Collect + for<'span> LookupSpan<'span>,
F: for<'writer> FormatFields<'writer> + 'static,
F: for<'visit, 'writer> FormatFields<'visit, 'writer> + 'static,
{
/// Notifies this subscriber that a new span was constructed with the given
/// `Attributes` and `Id`.
Expand Down Expand Up @@ -72,7 +72,7 @@ where

impl<C, F> ErrorSubscriber<C, F>
where
F: for<'writer> FormatFields<'writer> + 'static,
F: for<'visit, 'writer> FormatFields<'visit, 'writer> + 'static,
C: Collect + for<'span> LookupSpan<'span>,
{
/// Returns a new `ErrorSubscriber` with the provided [field formatter].
Expand Down
4 changes: 2 additions & 2 deletions tracing-journald/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl SpanVisitor<'_> {
}
}

impl Visit for SpanVisitor<'_> {
impl Visit<'_> for SpanVisitor<'_> {
fn record_str(&mut self, field: &Field, value: &str) {
self.put_span_prefix();
put_field_length_encoded(self.buf, field.name(), |buf| {
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<'a> EventVisitor<'a> {
}
}

impl Visit for EventVisitor<'_> {
impl Visit<'_> for EventVisitor<'_> {
fn record_str(&mut self, field: &Field, value: &str) {
self.put_prefix(field);
put_field_length_encoded(self.buf, field.name(), |buf| {
Expand Down
2 changes: 1 addition & 1 deletion tracing-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ impl<'a> LogVisitor<'a> {
}
}

impl<'a> Visit for LogVisitor<'a> {
impl<'a> Visit<'_> for LogVisitor<'a> {
fn record_debug(&mut self, _field: &Field, _value: &dyn fmt::Debug) {}

fn record_u64(&mut self, field: &Field, value: u64) {
Expand Down
4 changes: 2 additions & 2 deletions tracing-mock/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ pub struct CheckVisitor<'a> {
collector_name: &'a str,
}

impl<'a> Visit for CheckVisitor<'a> {
impl<'a> Visit<'_> for CheckVisitor<'a> {
fn record_f64(&mut self, field: &Field, value: f64) {
self.expect
.compare_or_panic(field.name(), &value, self.ctx, self.collector_name)
Expand Down Expand Up @@ -229,7 +229,7 @@ impl<'a> From<&'a dyn Value> for MockValue {
value: Option<MockValue>,
}

impl Visit for MockValueBuilder {
impl Visit<'_> for MockValueBuilder {
fn record_f64(&mut self, _: &Field, value: f64) {
self.value = Some(MockValue::F64(value));
}
Expand Down
4 changes: 2 additions & 2 deletions tracing-opentelemetry/src/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ fn str_to_status_code(s: &str) -> Option<otel::StatusCode> {

struct SpanEventVisitor<'a>(&'a mut otel::Event);

impl<'a> field::Visit for SpanEventVisitor<'a> {
impl<'a> field::Visit<'_> for SpanEventVisitor<'a> {
/// Record events on the underlying OpenTelemetry [`Span`] from `bool` values.
///
/// [`Span`]: opentelemetry::trace::Span
Expand Down Expand Up @@ -204,7 +204,7 @@ impl<'a> SpanAttributeVisitor<'a> {
}
}

impl<'a> field::Visit for SpanAttributeVisitor<'a> {
impl<'a> field::Visit<'_> for SpanAttributeVisitor<'a> {
/// Set attributes on the underlying OpenTelemetry [`Span`] from `bool` values.
///
/// [`Span`]: opentelemetry::trace::Span
Expand Down
4 changes: 2 additions & 2 deletions tracing-serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ where
}
}

impl<S> Visit for SerdeMapVisitor<S>
impl<S> Visit<'_> for SerdeMapVisitor<S>
where
S: SerializeMap,
{
Expand Down Expand Up @@ -423,7 +423,7 @@ pub struct SerdeStructVisitor<S: SerializeStruct> {
state: Result<(), S::Error>,
}

impl<S> Visit for SerdeStructVisitor<S>
impl<S> Visit<'_> for SerdeStructVisitor<S>
where
S: SerializeStruct,
{
Expand Down
Loading

0 comments on commit 557103e

Please sign in to comment.