about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-09-18 03:33:53 +0000
committerbors <bors@rust-lang.org>2023-09-18 03:33:53 +0000
commitdf99bc151a5dcf14aa1a0768a9f370e343aac466 (patch)
treea53a7a749beaba32fd6e8209c1b43bb2544961ef
parent10b88f8fa225fc10287793a24b163429c45cf8c0 (diff)
parent78846d17c184dcc1f1714689eaa370e1d472c489 (diff)
downloadrust-df99bc151a5dcf14aa1a0768a9f370e343aac466.tar.gz
rust-df99bc151a5dcf14aa1a0768a9f370e343aac466.zip
Auto merge of #108043 - a1phyr:string_write_fmt, r=workingjubilee
Small wins for formatting-related code

This PR does two small wins in fmt code:
- Override `write_char` for `PadAdapter` to use inner buffer's `write_char`
- Override some `write_fmt` implementations to avoid avoid the additional indirection and vtable generated by the default impl.
-rw-r--r--library/core/src/fmt/mod.rs24
1 files changed, 22 insertions, 2 deletions
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 9ce6093f1d1..8204b3855bd 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -188,8 +188,28 @@ pub trait Write {
     /// assert_eq!(&buf, "world");
     /// ```
     #[stable(feature = "rust1", since = "1.0.0")]
-    fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result {
-        write(&mut self, args)
+    fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
+        // We use a specialization for `Sized` types to avoid an indirection
+        // through `&mut self`
+        trait SpecWriteFmt {
+            fn spec_write_fmt(self, args: Arguments<'_>) -> Result;
+        }
+
+        impl<W: Write + ?Sized> SpecWriteFmt for &mut W {
+            #[inline]
+            default fn spec_write_fmt(mut self, args: Arguments<'_>) -> Result {
+                write(&mut self, args)
+            }
+        }
+
+        impl<W: Write> SpecWriteFmt for &mut W {
+            #[inline]
+            fn spec_write_fmt(self, args: Arguments<'_>) -> Result {
+                write(self, args)
+            }
+        }
+
+        self.spec_write_fmt(args)
     }
 }