Replace dyn fmt::Write with fmt::Formatter
What changed, and why it matters
This commit is a routine performance and code-quality refactor. It replaces dynamically-dispatched formatter parameters with concrete Rust formatter types so the compiler can inline the code. There is no security-relevant change: no new inputs are accepted, no validation logic changes, and no sensitive data handling is altered.
No security action needed. Treat as a normal performance refactor during dependency update review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch changes public/internal helper functions from accepting &mut dyn fmt::Write to accepting &mut fmt::Formatter. It updates callers in WifKey::to_wif() and the amount formatting tests to use fmt::Display adapters instead of writing to a String directly. This removes virtual dispatch and enables inlining, but does not modify any parsing, serialization bounds, cryptographic operations, or error handling.
Changed components
crypto/src/key.rsunits/src/amount/mod.rsunits/src/amount/tests.rsInspect captured patch +18 / −13
diff --git a/crypto/src/key.rs b/crypto/src/key.rs
index d36f1cf0..3d00c8f7 100644
--- a/crypto/src/key.rs
+++ b/crypto/src/key.rs
@@ -6,7 +6,7 @@
//! (de)serialized.
#[cfg(feature = "alloc")]
-use alloc::string::String;
+use alloc::string::{String, ToString as _};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::borrow::Borrow;
@@ -1168,7 +1168,7 @@ impl WifKey {
#[rustfmt::skip]
#[cfg(feature = "alloc")]
#[inline]
- pub fn fmt_wif(&self, fmt: &mut dyn fmt::Write) -> fmt::Result {
+ pub fn fmt_wif(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let mut ret = [0; 34];
ret[0] = if self.network_kind.is_mainnet() { 128 } else { 239 };
@@ -1186,10 +1186,11 @@ impl WifKey {
#[cfg(feature = "alloc")]
#[inline]
pub fn to_wif(&self) -> String {
- let mut buf = String::new();
- let _ = self.fmt_wif(&mut buf);
- buf.shrink_to_fit();
- buf
+ struct WifString<'a>(&'a WifKey);
+ impl fmt::Display for WifString<'_> {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt_wif(f) }
+ }
+ WifString(self).to_string()
}
/// Parses the WIF encoded private key.
diff --git a/units/src/amount/mod.rs b/units/src/amount/mod.rs
index e2e4d7ed..7c2809f8 100644
--- a/units/src/amount/mod.rs
+++ b/units/src/amount/mod.rs
@@ -19,7 +19,7 @@ pub mod serde;
use core::cmp::Ordering;
use core::convert::Infallible;
-use core::fmt;
+use core::fmt::{self, Write as _};
use core::str::FromStr;
#[cfg(feature = "arbitrary")]
@@ -439,7 +439,7 @@ fn dec_width(mut num: u64) -> usize {
width
}
-fn repeat_char(f: &mut dyn fmt::Write, c: char, count: usize) -> fmt::Result {
+fn repeat_char(f: &mut fmt::Formatter, c: char, count: usize) -> fmt::Result {
for _ in 0..count {
f.write_char(c)?;
}
@@ -450,7 +450,7 @@ fn repeat_char(f: &mut dyn fmt::Write, c: char, count: usize) -> fmt::Result {
fn fmt_satoshi_in(
mut satoshi: u64,
negative: bool,
- f: &mut dyn fmt::Write,
+ f: &mut fmt::Formatter,
denom: Denomination,
show_denom: bool,
options: FormatOptions,
diff --git a/units/src/amount/tests.rs b/units/src/amount/tests.rs
index b613fe85..9989ef7b 100644
--- a/units/src/amount/tests.rs
+++ b/units/src/amount/tests.rs
@@ -5,7 +5,7 @@
#[cfg(feature = "alloc")]
use alloc::format;
#[cfg(feature = "alloc")]
-use alloc::string::{String, ToString};
+use alloc::string::ToString;
use core::num::{NonZeroI64, NonZeroU64};
#[cfg(feature = "std")]
use std::panic;
@@ -528,10 +528,14 @@ fn to_string() {
#[test]
#[cfg(feature = "alloc")]
fn test_repeat_char() {
- let mut buf = String::new();
- repeat_char(&mut buf, '0', 0).unwrap();
+ struct Repeat(char, usize);
+ impl fmt::Display for Repeat {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { repeat_char(f, self.0, self.1) }
+ }
+
+ let buf = Repeat('0', 0).to_string();
assert_eq!(buf.len(), 0);
- repeat_char(&mut buf, '0', 42).unwrap();
+ let buf = Repeat('0', 42).to_string();
assert_eq!(buf.len(), 42);
assert!(buf.chars().all(|c| c == '0'));
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.