From 15b028c5859f98b32cde69b81dd62468039db0be Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 22 Apr 2015 14:18:02 +0200 Subject: Add a write_char method to std::fmt::Formatter. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the logical next step after #24661, but I’m less sure about this one. --- src/libcore/fmt/mod.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index ee1cab4076d..34d635ca4ab 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -731,6 +731,13 @@ impl<'a> Formatter<'a> { self.buf.write_str(data) } + /// Writes a `char` to the underlying buffer contained within this + /// formatter. + #[stable(feature = "fmt_write_char", since = "1.1.0")] + pub fn write_char(&mut self, c: char) -> Result { + self.buf.write_char(c) + } + /// Writes some formatted information into this instance #[stable(feature = "rust1", since = "1.0.0")] pub fn write_fmt(&mut self, fmt: Arguments) -> Result { @@ -965,10 +972,7 @@ impl Debug for char { #[stable(feature = "rust1", since = "1.0.0")] impl Display for char { fn fmt(&self, f: &mut Formatter) -> Result { - let mut utf8 = [0; 4]; - let amt = self.encode_utf8(&mut utf8).unwrap_or(0); - let s: &str = unsafe { mem::transmute(&utf8[..amt]) }; - Display::fmt(s, f) + f.write_char(*self) } } -- cgit 1.4.1-3-g733a5 From 63da18b269128c6594b0fa60064b187a9b5d0418 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Wed, 10 Jun 2015 21:41:34 +0200 Subject: Have std::fmt::Formatter implement std::fmt::Write. --- src/libcore/fmt/mod.rs | 22 +++++++++++++++------- src/test/run-pass/ifmt.rs | 2 +- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 34d635ca4ab..da4d24bdc7b 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -731,13 +731,6 @@ impl<'a> Formatter<'a> { self.buf.write_str(data) } - /// Writes a `char` to the underlying buffer contained within this - /// formatter. - #[stable(feature = "fmt_write_char", since = "1.1.0")] - pub fn write_char(&mut self, c: char) -> Result { - self.buf.write_char(c) - } - /// Writes some formatted information into this instance #[stable(feature = "rust1", since = "1.0.0")] pub fn write_fmt(&mut self, fmt: Arguments) -> Result { @@ -899,6 +892,21 @@ impl<'a> Formatter<'a> { } } +#[stable(since = "1.2.0", feature = "formatter_write")] +impl<'a> Write for Formatter<'a> { + fn write_str(&mut self, s: &str) -> Result { + self.buf.write_str(s) + } + + fn write_char(&mut self, c: char) -> Result { + self.buf.write_char(c) + } + + fn write_fmt(&mut self, args: Arguments) -> Result { + write(self.buf, args) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Display for Error { fn fmt(&self, f: &mut Formatter) -> Result { diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs index abd1a953113..c8adb6ccc0a 100644 --- a/src/test/run-pass/ifmt.rs +++ b/src/test/run-pass/ifmt.rs @@ -15,7 +15,7 @@ #![allow(unknown_features)] #![feature(box_syntax)] -use std::fmt; +use std::fmt::{self, Write}; use std::usize; struct A; -- cgit 1.4.1-3-g733a5