]> git.lizzy.rs Git - rust.git/commitdiff
Auto merge of #26698 - alexcrichton:char-fmt, r=huonw
authorbors <bors@rust-lang.org>
Wed, 1 Jul 2015 03:35:46 +0000 (03:35 +0000)
committerbors <bors@rust-lang.org>
Wed, 1 Jul 2015 03:35:46 +0000 (03:35 +0000)
This recently regressed in #24689, and this updates the `Display` implementation
to take formatting flags into account.

Closes #26625

1  2 
src/libcore/fmt/mod.rs

diff --combined src/libcore/fmt/mod.rs
index 35dea6d15f0e9cef79a5df1324e16d345064ab82,343772c764f817b242c7856dd8b025c35cc2c949..f735ed7b78b17b5babe8723ecf6a5720dac2c5a7
@@@ -267,7 -267,7 +267,7 @@@ impl<'a> Display for Arguments<'a> 
      }
  }
  
 -/// Format trait for the `:?` format. Useful for debugging, all types
 +/// Format trait for the `?` character. Useful for debugging, all types
  /// should implement this.
  ///
  /// Generally speaking, you should just `derive` a `Debug` implementation.
  /// There are a number of `debug_*` methods on `Formatter` to help you with manual
  /// implementations, such as [`debug_struct`][debug_struct].
  ///
 +/// `Debug` implementations using either `derive` or the debug builder API
 +/// on `Formatter` support pretty printing using the alternate flag: `{:#?}`.
 +///
  /// [debug_struct]: ../std/fmt/struct.Formatter.html#method.debug_struct
  #[stable(feature = "rust1", since = "1.0.0")]
  #[rustc_on_unimplemented = "`{Self}` cannot be formatted using `:?`; if it is \
@@@ -983,7 -980,14 +983,14 @@@ impl Debug for char 
  #[stable(feature = "rust1", since = "1.0.0")]
  impl Display for char {
      fn fmt(&self, f: &mut Formatter) -> Result {
-         f.write_char(*self)
+         if f.width.is_none() && f.precision.is_none() {
+             f.write_char(*self)
+         } else {
+             let mut utf8 = [0; 4];
+             let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
+             let s: &str = unsafe { mem::transmute(&utf8[..amt]) };
+             f.pad(s)
+         }
      }
  }