summaryrefslogtreecommitdiffstats
path: root/library/core/src/fmt
diff options
context:
space:
mode:
Diffstat (limited to 'library/core/src/fmt')
-rw-r--r--library/core/src/fmt/builders.rs8
-rw-r--r--library/core/src/fmt/mod.rs28
2 files changed, 32 insertions, 4 deletions
diff --git a/library/core/src/fmt/builders.rs b/library/core/src/fmt/builders.rs
index d2c9f9800..922724804 100644
--- a/library/core/src/fmt/builders.rs
+++ b/library/core/src/fmt/builders.rs
@@ -40,6 +40,14 @@ impl fmt::Write for PadAdapter<'_, '_> {
Ok(())
}
+
+ fn write_char(&mut self, c: char) -> fmt::Result {
+ if self.state.on_newline {
+ self.buf.write_str(" ")?;
+ }
+ self.state.on_newline = c == '\n';
+ self.buf.write_char(c)
+ }
}
/// A struct to help with [`fmt::Debug`](Debug) implementations.
diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs
index 9ce6093f1..fc91d1afc 100644
--- a/library/core/src/fmt/mod.rs
+++ b/library/core/src/fmt/mod.rs
@@ -112,9 +112,9 @@ pub trait Write {
///
/// # Errors
///
- /// This function will return an instance of [`Error`] on error.
+ /// This function will return an instance of [`std::fmt::Error`][Error] on error.
///
- /// The purpose of std::fmt::Error is to abort the formatting operation when the underlying
+ /// The purpose of that error is to abort the formatting operation when the underlying
/// destination encounters some error preventing it from accepting more text; it should
/// generally be propagated rather than handled, at least when implementing formatting traits.
///
@@ -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)
}
}