From 034dd1277e8bb247efc109e30943d3d97c938270 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:00:37 +0100 Subject: [PATCH] perf(anstyle): avoid using write! macro with single `str` --- crates/anstyle/src/color.rs | 10 ++++------ crates/anstyle/src/effect.rs | 3 +-- crates/anstyle/src/reset.rs | 2 +- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/crates/anstyle/src/color.rs b/crates/anstyle/src/color.rs index a9aafb5a..b930a020 100644 --- a/crates/anstyle/src/color.rs +++ b/crates/anstyle/src/color.rs @@ -627,19 +627,17 @@ impl DisplayBuffer { impl core::fmt::Display for DisplayBuffer { #[inline] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - let s = self.as_str(); - write!(f, "{s}") + f.write_str(self.as_str()) } } #[derive(Copy, Clone, Default, Debug)] -struct NullFormatter(D); +struct NullFormatter(&'static str); -impl core::fmt::Display for NullFormatter { +impl core::fmt::Display for NullFormatter { #[inline] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - let d = &self.0; - write!(f, "{d}") + f.write_str(self.0) } } diff --git a/crates/anstyle/src/effect.rs b/crates/anstyle/src/effect.rs index 1a80fa3e..507dfc83 100644 --- a/crates/anstyle/src/effect.rs +++ b/crates/anstyle/src/effect.rs @@ -323,8 +323,7 @@ impl core::fmt::Display for EffectsDisplay { #[inline] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { for index in self.0.index_iter() { - let escape = METADATA[index].escape; - write!(f, "{escape}")?; + f.write_str(METADATA[index].escape)?; } Ok(()) } diff --git a/crates/anstyle/src/reset.rs b/crates/anstyle/src/reset.rs index 62c8b4bc..5a4a5f75 100644 --- a/crates/anstyle/src/reset.rs +++ b/crates/anstyle/src/reset.rs @@ -15,7 +15,7 @@ impl Reset { impl core::fmt::Display for Reset { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "{RESET}") + f.write_str(RESET) } }