summary refs log tree commit diff
path: root/src/libcore/fmt
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2015-04-21 14:51:28 +0200
committerSimon Sapin <simon.sapin@exyr.org>2015-04-21 14:51:28 +0200
commit265a7cc3bde71bf34f6b2cfea6353ede20bfd7a5 (patch)
tree4be7c92a6247ea3f29af2deb219c12b321275dca /src/libcore/fmt
parent3860240b0e124f38483ea4bd070b61d362871ece (diff)
downloadrust-265a7cc3bde71bf34f6b2cfea6353ede20bfd7a5.tar.gz
rust-265a7cc3bde71bf34f6b2cfea6353ede20bfd7a5.zip
Add a `write_char` method to `std::fmt::Write`
as accepted in [RFC 526](https://github.com/rust-lang/rfcs/blob/master/text/0526-fmt-text-writer.md).
Diffstat (limited to 'src/libcore/fmt')
-rw-r--r--src/libcore/fmt/mod.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 80c661b260c..85226444e84 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -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 = "rust1", since = "1.0.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