rollup merge of #19040: alexcrichton/issue-18904

This commit applies the stabilization of std::fmt as outlined in [RFC 380][rfc].
There are a number of breaking changes as a part of this commit which will need
to be handled to migrated old code:

* A number of formatting traits have been removed: String, Bool, Char, Unsigned,
  Signed, and Float. It is recommended to instead use Show wherever possible or
  to use adaptor structs to implement other methods of formatting.

* The format specifier for Boolean has changed from `t` to `b`.

* The enum `FormatError` has been renamed to `Error` as well as becoming a unit
  struct instead of an enum. The `WriteError` variant no longer exists.

* The `format_args_method!` macro has been removed with no replacement. Alter
  code to use the `format_args!` macro instead.

* The public fields of a `Formatter` have become read-only with no replacement.
  Use a new formatting string to alter the formatting flags in combination with
  the `write!` macro. The fields can be accessed through accessor methods on the
  `Formatter` structure.

Other than these breaking changes, the contents of std::fmt should now also all
contain stability markers. Most of them are still #[unstable] or #[experimental]

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0380-stabilize-std-fmt.md
[breaking-change]

Closes #18904
This commit is contained in:
Jakub Bukaj 2014-11-19 22:38:26 +01:00
commit a22f06db19
48 changed files with 291 additions and 347 deletions

View file

@ -148,7 +148,7 @@ fn write_header(header: &str) {
}
fn write_row(label: &str, value: Duration) {
println!("{:30s} {} s\n", label, value);
println!("{:30} {} s\n", label, value);
}
fn write_results(label: &str, results: &Results) {

View file

@ -115,7 +115,7 @@ fn main() {
for y in range(0u, 256) {
for x in range(0u, 256) {
let idx = (pixels[y*256+x] / 0.2) as uint;
print!("{:c}", symbols[idx]);
print!("{}", symbols[idx]);
}
print!("\n");
}

View file

@ -63,7 +63,7 @@ fn sort_and_fmt(mm: &HashMap<Vec<u8> , uint>, total: uint) -> String {
let mut buffer = String::new();
for &(ref k, v) in pairs_sorted.iter() {
buffer.push_str(format!("{} {:0.3f}\n",
buffer.push_str(format!("{} {:0.3}\n",
k.as_slice()
.to_ascii()
.to_uppercase()

View file

@ -266,7 +266,7 @@ fn print_frequencies(frequencies: &Table, frame: uint) {
}
for &(count, key) in vector.iter().rev() {
println!("{} {:.3f}",
println!("{} {:.3}",
key.unpack(frame).as_slice(),
(count as f32 * 100.0) / (total_count as f32));
}

View file

@ -179,11 +179,11 @@ fn main() {
let mut bodies = BODIES;
offset_momentum(&mut bodies);
println!("{:.9f}", energy(&bodies));
println!("{:.9}", energy(&bodies));
advance(&mut bodies, 0.01, n);
println!("{:.9f}", energy(&bodies));
println!("{:.9}", energy(&bodies));
}
/// Pop a mutable reference off the head of a slice, mutating the slice to no

View file

@ -59,7 +59,7 @@ fn main() {
} else {
from_str(args[1].as_slice()).unwrap()
});
println!("{:.9f}", answer);
println!("{:.9}", answer);
}
fn spectralnorm(n: uint) -> f64 {

View file

@ -23,8 +23,8 @@ fn main() {
format!("{foo}", 1, foo=2); //~ ERROR: argument never used
format!("", foo=2); //~ ERROR: named argument never used
format!("{0:d} {0:s}", 1); //~ ERROR: redeclared with type `s`
format!("{foo:d} {foo:s}", foo=1); //~ ERROR: redeclared with type `s`
format!("{0:x} {0:X}", 1); //~ ERROR: redeclared with type `X`
format!("{foo:x} {foo:X}", foo=1); //~ ERROR: redeclared with type `X`
format!("{foo}", foo=1, foo=2); //~ ERROR: duplicate argument
format!("", foo=1, 2); //~ ERROR: positional arguments cannot follow

View file

@ -9,6 +9,6 @@
// except according to those terms.
fn main() {
format!("{:d}", "3");
//~^ ERROR: the trait `core::fmt::Signed` is not implemented
format!("{:X}", "3");
//~^ ERROR: the trait `core::fmt::UpperHex` is not implemented
}

View file

@ -13,5 +13,5 @@
fn foo(a: uint) -> uint { a }
fn main() {
println!("{:u}", foo(10i)); //~ ERROR mismatched types
println!("{}", foo(10i)); //~ ERROR mismatched types
}

View file

@ -10,17 +10,18 @@
use std::fmt::Show;
trait Str {}
trait Something {
fn yay<T: Show>(_: Option<Self>, thing: &[T]) -> String {
}
fn yay<T: Show>(_: Option<Self>, thing: &[T]);
}
struct X { data: u32 }
impl Something for X {
fn yay<T: Str>(_:Option<X>, thing: &[T]) -> String {
//~^ ERROR in method `yay`, type parameter 0 requires bound `core::str::Str`, which is not required
format!("{:s}", thing[0])
fn yay<T: Str>(_:Option<X>, thing: &[T]) {
//~^ ERROR in method `yay`, type parameter 0 requires bound `Str`, which is not required
}
}

View file

@ -22,12 +22,12 @@ struct A;
struct B;
struct C;
impl fmt::Signed for A {
impl fmt::LowerHex for A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write("aloha".as_bytes())
}
}
impl fmt::Signed for B {
impl fmt::UpperHex for B {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write("adios".as_bytes())
}
@ -55,71 +55,71 @@ pub fn main() {
t!(format!("{}", 'a'), "a");
// At least exercise all the formats
t!(format!("{:b}", true), "true");
t!(format!("{:c}", '☃'), "");
t!(format!("{:d}", 10i), "10");
t!(format!("{:i}", 10i), "10");
t!(format!("{:u}", 10u), "10");
t!(format!("{}", true), "true");
t!(format!("{}", '☃'), "");
t!(format!("{}", 10i), "10");
t!(format!("{}", 10i), "10");
t!(format!("{}", 10u), "10");
t!(format!("{:o}", 10u), "12");
t!(format!("{:x}", 10u), "a");
t!(format!("{:X}", 10u), "A");
t!(format!("{:s}", "foo"), "foo");
t!(format!("{:s}", "foo".to_string()), "foo");
t!(format!("{}", "foo"), "foo");
t!(format!("{}", "foo".to_string()), "foo");
t!(format!("{:p}", 0x1234 as *const int), "0x1234");
t!(format!("{:p}", 0x1234 as *mut int), "0x1234");
t!(format!("{:d}", A), "aloha");
t!(format!("{:d}", B), "adios");
t!(format!("foo {:s} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
t!(format!("{:x}", A), "aloha");
t!(format!("{:X}", B), "adios");
t!(format!("foo {} ☃☃☃☃☃☃", "bar"), "foo bar ☃☃☃☃☃☃");
t!(format!("{1} {0}", 0i, 1i), "1 0");
t!(format!("{foo} {bar}", foo=0i, bar=1i), "0 1");
t!(format!("{foo} {1} {bar} {0}", 0i, 1i, foo=2i, bar=3i), "2 1 3 0");
t!(format!("{} {0}", "a"), "a a");
t!(format!("{foo_bar}", foo_bar=1i), "1");
t!(format!("{:d}", 5i + 5i), "10");
t!(format!("{}", 5i + 5i), "10");
t!(format!("{:#4}", C), "☃123");
let a: &fmt::Show = &1i;
t!(format!("{}", a), "1");
// Formatting strings and their arguments
t!(format!("{:s}", "a"), "a");
t!(format!("{:4s}", "a"), "a ");
t!(format!("{:4s}", ""), "");
t!(format!("{:>4s}", "a"), " a");
t!(format!("{:<4s}", "a"), "a ");
t!(format!("{:^5s}", "a"), " a ");
t!(format!("{:^5s}", "aa"), " aa ");
t!(format!("{:^4s}", "a"), " a ");
t!(format!("{:^4s}", "aa"), " aa ");
t!(format!("{:.4s}", "a"), "a");
t!(format!("{:4.4s}", "a"), "a ");
t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:^4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaa"), "aaaa");
t!(format!("{:2.4s}", "aaa"), "aaa");
t!(format!("{:2.4s}", "aa"), "aa");
t!(format!("{:2.4s}", "a"), "a ");
t!(format!("{:0>2s}", "a"), "0a");
t!(format!("{:.*s}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:.1$s}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
t!(format!("{:.a$s}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
t!(format!("{:1$s}", "a", 4), "a ");
t!(format!("{1:0$s}", 4, "a"), "a ");
t!(format!("{:a$s}", "a", a=4), "a ");
t!(format!("{:-#s}", "a"), "a");
t!(format!("{:+#s}", "a"), "a");
t!(format!("{}", "a"), "a");
t!(format!("{:4}", "a"), "a ");
t!(format!("{:4}", ""), "");
t!(format!("{:>4}", "a"), " a");
t!(format!("{:<4}", "a"), "a ");
t!(format!("{:^5}", "a"), " a ");
t!(format!("{:^5}", "aa"), " aa ");
t!(format!("{:^4}", "a"), " a ");
t!(format!("{:^4}", "aa"), " aa ");
t!(format!("{:.4}", "a"), "a");
t!(format!("{:4.4}", "a"), "a ");
t!(format!("{:4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:2.4}", "aaaaa"), "aaaa");
t!(format!("{:2.4}", "aaaa"), "aaaa");
t!(format!("{:2.4}", "aaa"), "aaa");
t!(format!("{:2.4}", "aa"), "aa");
t!(format!("{:2.4}", "a"), "a ");
t!(format!("{:0>2}", "a"), "0a");
t!(format!("{:.*}", 4, "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:.1$}", "aaaaaaaaaaaaaaaaaa", 4), "aaaa");
t!(format!("{:.a$}", "aaaaaaaaaaaaaaaaaa", a=4), "aaaa");
t!(format!("{:1$}", "a", 4), "a ");
t!(format!("{1:0$}", 4, "a"), "a ");
t!(format!("{:a$}", "a", a=4), "a ");
t!(format!("{:-#}", "a"), "a");
t!(format!("{:+#}", "a"), "a");
// Some float stuff
t!(format!("{:f}", 1.0f32), "1");
t!(format!("{:f}", 1.0f64), "1");
t!(format!("{:.3f}", 1.0f64), "1.000");
t!(format!("{:10.3f}", 1.0f64), " 1.000");
t!(format!("{:+10.3f}", 1.0f64), " +1.000");
t!(format!("{:+10.3f}", -1.0f64), " -1.000");
t!(format!("{:}", 1.0f32), "1");
t!(format!("{:}", 1.0f64), "1");
t!(format!("{:.3}", 1.0f64), "1.000");
t!(format!("{:10.3}", 1.0f64), " 1.000");
t!(format!("{:+10.3}", 1.0f64), " +1.000");
t!(format!("{:+10.3}", -1.0f64), " -1.000");
t!(format!("{:e}", 1.2345e6f32), "1.2345e6");
t!(format!("{:e}", 1.2345e6f64), "1.2345e6");
@ -164,7 +164,7 @@ fn test_write() {
{
let w = &mut buf as &mut io::Writer;
write!(w, "{foo}", foo=4i);
write!(w, "{:s}", "hello");
write!(w, "{}", "hello");
writeln!(w, "{}", "line");
writeln!(w, "{foo}", foo="bar");
}

View file

@ -46,19 +46,19 @@ unsafe fn test_triangle() -> bool {
static PRINT : bool = false;
unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
if PRINT { println!("allocate(size={:u} align={:u})", size, align); }
if PRINT { println!("allocate(size={} align={})", size, align); }
let ret = heap::allocate(size, align);
if ret.is_null() { alloc::oom() }
if PRINT { println!("allocate(size={:u} align={:u}) ret: 0x{:010x}",
if PRINT { println!("allocate(size={} align={}) ret: 0x{:010x}",
size, align, ret as uint);
}
ret
}
unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
if PRINT { println!("deallocate(ptr=0x{:010x} size={:u} align={:u})",
if PRINT { println!("deallocate(ptr=0x{:010x} size={} align={})",
ptr as uint, size, align);
}
@ -66,7 +66,7 @@ unsafe fn test_triangle() -> bool {
}
unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
if PRINT {
println!("reallocate(ptr=0x{:010x} old_size={:u} size={:u} align={:u})",
println!("reallocate(ptr=0x{:010x} old_size={} size={} align={})",
ptr as uint, old_size, size, align);
}
@ -74,7 +74,7 @@ unsafe fn test_triangle() -> bool {
if ret.is_null() { alloc::oom() }
if PRINT {
println!("reallocate(ptr=0x{:010x} old_size={:u} size={:u} align={:u}) \
println!("reallocate(ptr=0x{:010x} old_size={} size={} align={}) \
ret: 0x{:010x}",
ptr as uint, old_size, size, align, ret as uint);
}