rollup merge of #24661: SimonSapin/fmt-write-char
as accepted in [RFC 526](https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md). Note that this brand new method is marked as **stable**. I judged this safe enough: it’s simple enough that it’s very unlikely to change. Still, I can mark it unstable instead if you prefer. r? @alexcrichton
This commit is contained in:
commit
c7017b3b3e
|
@ -1082,4 +1082,10 @@ impl fmt::Write for String {
|
|||
self.push_str(s);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn write_char(&mut self, c: char) -> fmt::Result {
|
||||
self.push(c);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,6 +83,23 @@ pub trait Write {
|
|||
#[stable(feature = "rust1", since = "1.0.0")]
|
||||
fn write_str(&mut self, s: &str) -> Result;
|
||||
|
||||
/// Writes a `char` into this writer, returning whether the write succeeded.
|
||||
///
|
||||
/// A single `char` may be encoded as more than one byte.
|
||||
/// This method can only succeed if the entire byte sequence was successfully
|
||||
/// written, and this method will not return until all data has been
|
||||
/// written or an error occurs.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return an instance of `FormatError` on error.
|
||||
#[stable(feature = "fmt_write_char", since = "1.1.0")]
|
||||
fn write_char(&mut self, c: char) -> Result {
|
||||
let mut utf_8 = [0u8; 4];
|
||||
let bytes_written = c.encode_utf8(&mut utf_8).unwrap_or(0);
|
||||
self.write_str(unsafe { mem::transmute(&utf_8[..bytes_written]) })
|
||||
}
|
||||
|
||||
/// Glue for usage of the `write!` macro with implementers of this trait.
|
||||
///
|
||||
/// This method should generally not be invoked manually, but rather through
|
||||
|
|
Loading…
Reference in New Issue