Skip to content

Commit 3524310

Browse files
hrxidavidbarsky
authored andcommitted
core: avoid unnecessary format_args! and write! macros (#1988)
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>
1 parent e225c82 commit 3524310

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

tracing-core/src/field.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub trait Visit {
301301
#[cfg(feature = "std")]
302302
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
303303
fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) {
304-
self.record_debug(field, &format_args!("{}", value))
304+
self.record_debug(field, &DisplayValue(value))
305305
}
306306

307307
/// Visit a value implementing `fmt::Debug`.
@@ -599,19 +599,19 @@ where
599599
T: fmt::Display,
600600
{
601601
fn record(&self, key: &Field, visitor: &mut dyn Visit) {
602-
visitor.record_debug(key, &format_args!("{}", self.0))
602+
visitor.record_debug(key, self)
603603
}
604604
}
605605

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

612612
impl<T: fmt::Display> fmt::Display for DisplayValue<T> {
613613
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
614-
fmt::Display::fmt(&self.0, f)
614+
self.0.fmt(f)
615615
}
616616
}
617617

@@ -630,7 +630,7 @@ where
630630

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

0 commit comments

Comments
 (0)