Optimize io::Write::write_fmt for constant strings

When the formatting args to `fmt::Write::write_fmt` are a statically
known string, it simplifies to only calling `write_str` without a
runtime branch. Do the same in `io::Write::write_fmt` with `write_all`.

Also, match the convention of `fmt::Write` for the name of `args`.
This commit is contained in:
Thalia Archibald
2025-03-01 12:34:09 -08:00
parent 493c38ba37
commit a0c3dd4df4
2 changed files with 48 additions and 36 deletions

View File

@@ -710,9 +710,10 @@ impl<'a> Arguments<'a> {
}
/// Same as [`Arguments::as_str`], but will only return `Some(s)` if it can be determined at compile time.
#[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
#[must_use]
#[inline]
fn as_statically_known_str(&self) -> Option<&'static str> {
pub fn as_statically_known_str(&self) -> Option<&'static str> {
let s = self.as_str();
if core::intrinsics::is_val_statically_known(s.is_some()) { s } else { None }
}