Skip to content

Commit 23904e5

Browse files
committed
Leverage fmt::Arguments::as_str in the write! macro
1 parent 86c6ebe commit 23904e5

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

library/core/src/fmt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ pub trait Write {
189189
/// ```
190190
#[stable(feature = "rust1", since = "1.0.0")]
191191
fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result {
192-
write(&mut self, args)
192+
if let Some(s) = args.as_str() { self.write_str(s) } else { write(&mut self, args) }
193193
}
194194
}
195195

library/std/src/io/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,10 @@ pub trait Write {
16491649
/// ```
16501650
#[stable(feature = "rust1", since = "1.0.0")]
16511651
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
1652+
if let Some(s) = fmt.as_str() {
1653+
return self.write_all(s.as_bytes());
1654+
}
1655+
16521656
// Create a shim which translates a Write to a fmt::Write and saves
16531657
// off I/O errors. instead of discarding them
16541658
struct Adapter<'a, T: ?Sized + 'a> {

0 commit comments

Comments
 (0)