Skip to content

Commit

Permalink
perf: avoid using write! macro with single str
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Nov 1, 2024
1 parent 5628f47 commit fb843c5
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
10 changes: 4 additions & 6 deletions crates/anstyle/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: core::fmt::Display>(D);
struct NullFormatter(&'static str);

impl<D: core::fmt::Display> core::fmt::Display for NullFormatter<D> {
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)
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/anstyle/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle/src/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit fb843c5

Please sign in to comment.