Skip to content

Commit

Permalink
core: avoid unnecessary format_args! and write! macros (#1988)
Browse files Browse the repository at this point in the history
This branch removes some unnecessary uses of the `format_args!`
and `write!` macros in `tracing-core`. Using `fmt::Display::fmt` and
similar rather than the macros may be slightly more efficient.

Co-authored-by: David Barsky <me@davidbarsky.com>
  • Loading branch information
hrxi and davidbarsky committed Mar 16, 2022
1 parent b16dc03 commit 989fb62
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ pub trait Visit {
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {
self.record_debug(field, &format_args!("{}", value))
self.record_debug(field, &DisplayValue(value))
}

/// Visit a value implementing `fmt::Debug`.
Expand Down Expand Up @@ -499,19 +499,19 @@ where
T: fmt::Display,
{
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
visitor.record_debug(key, &format_args!("{}", self.0))
visitor.record_debug(key, self)
}
}

impl<T: fmt::Display> fmt::Debug for DisplayValue<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
fmt::Display::fmt(self, f)
}
}

impl<T: fmt::Display> fmt::Display for DisplayValue<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
self.0.fmt(f)
}
}

Expand All @@ -530,7 +530,7 @@ where

impl<T: fmt::Debug> fmt::Debug for DebugValue<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.0)
self.0.fmt(f)
}
}

Expand Down

0 comments on commit 989fb62

Please sign in to comment.