From 44440e5c18a1dbcc9685866ffffe00c508929079 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Sat, 20 Dec 2014 00:09:35 -0800 Subject: core: split into fmt::Show and fmt::String fmt::Show is for debugging, and can and should be implemented for all public types. This trait is used with `{:?}` syntax. There still exists #[derive(Show)]. fmt::String is for types that faithfully be represented as a String. Because of this, there is no way to derive fmt::String, all implementations must be purposeful. It is used by the default format syntax, `{}`. This will break most instances of `{}`, since that now requires the type to impl fmt::String. In most cases, replacing `{}` with `{:?}` is the correct fix. Types that were being printed specifically for users should receive a fmt::String implementation to fix this. Part of #20013 [breaking-change] --- src/libcoretest/any.rs | 16 ++++++++-------- src/libcoretest/cell.rs | 8 ++++---- src/libcoretest/fmt/num.rs | 24 ++++++++++++++++++++++++ src/libcoretest/result.rs | 8 ++++---- src/libcoretest/tuple.rs | 12 ++++++------ 5 files changed, 46 insertions(+), 22 deletions(-) (limited to 'src/libcoretest') diff --git a/src/libcoretest/any.rs b/src/libcoretest/any.rs index 9b0471bfad9..c0be3a28794 100644 --- a/src/libcoretest/any.rs +++ b/src/libcoretest/any.rs @@ -56,12 +56,12 @@ fn any_downcast_ref() { match a.downcast_ref::() { Some(&5) => {} - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match a.downcast_ref::() { None => {} - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } } @@ -79,7 +79,7 @@ fn any_downcast_mut() { assert_eq!(*x, 5u); *x = 612; } - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match b_r.downcast_mut::() { @@ -87,27 +87,27 @@ fn any_downcast_mut() { assert_eq!(*x, 7u); *x = 413; } - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match a_r.downcast_mut::() { None => (), - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match b_r.downcast_mut::() { None => (), - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match a_r.downcast_mut::() { Some(&612) => {} - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } match b_r.downcast_mut::() { Some(&413) => {} - x => panic!("Unexpected value {}", x) + x => panic!("Unexpected value {:?}", x) } } diff --git a/src/libcoretest/cell.rs b/src/libcoretest/cell.rs index 54da6264bb0..86f34ecd15e 100644 --- a/src/libcoretest/cell.rs +++ b/src/libcoretest/cell.rs @@ -29,10 +29,10 @@ fn smoketest_cell() { #[test] fn cell_has_sensible_show() { let x = Cell::new("foo bar"); - assert!(format!("{}", x).contains(x.get())); + assert!(format!("{:?}", x).contains(x.get())); x.set("baz qux"); - assert!(format!("{}", x).contains(x.get())); + assert!(format!("{:?}", x).contains(x.get())); } #[test] @@ -40,11 +40,11 @@ fn ref_and_refmut_have_sensible_show() { let refcell = RefCell::new("foo"); let refcell_refmut = refcell.borrow_mut(); - assert!(format!("{}", refcell_refmut).contains("foo")); + assert!(format!("{:?}", refcell_refmut).contains("foo")); drop(refcell_refmut); let refcell_ref = refcell.borrow(); - assert!(format!("{}", refcell_ref).contains("foo")); + assert!(format!("{:?}", refcell_ref).contains("foo")); drop(refcell_ref); } diff --git a/src/libcoretest/fmt/num.rs b/src/libcoretest/fmt/num.rs index 1e28933becd..c259e4cbb68 100644 --- a/src/libcoretest/fmt/num.rs +++ b/src/libcoretest/fmt/num.rs @@ -26,6 +26,11 @@ fn test_format_int() { assert!(format!("{}", -1i16) == "-1"); assert!(format!("{}", -1i32) == "-1"); assert!(format!("{}", -1i64) == "-1"); + assert!(format!("{:?}", 1i) == "1i"); + assert!(format!("{:?}", 1i8) == "1i8"); + assert!(format!("{:?}", 1i16) == "1i16"); + assert!(format!("{:?}", 1i32) == "1i32"); + assert!(format!("{:?}", 1i64) == "1i64"); assert!(format!("{:b}", 1i) == "1"); assert!(format!("{:b}", 1i8) == "1"); assert!(format!("{:b}", 1i16) == "1"); @@ -52,6 +57,11 @@ fn test_format_int() { assert!(format!("{}", 1u16) == "1"); assert!(format!("{}", 1u32) == "1"); assert!(format!("{}", 1u64) == "1"); + assert!(format!("{:?}", 1u) == "1u"); + assert!(format!("{:?}", 1u8) == "1u8"); + assert!(format!("{:?}", 1u16) == "1u16"); + assert!(format!("{:?}", 1u32) == "1u32"); + assert!(format!("{:?}", 1u64) == "1u64"); assert!(format!("{:b}", 1u) == "1"); assert!(format!("{:b}", 1u8) == "1"); assert!(format!("{:b}", 1u16) == "1"); @@ -84,12 +94,14 @@ fn test_format_int() { #[test] fn test_format_int_zero() { assert!(format!("{}", 0i) == "0"); + assert!(format!("{:?}", 0i) == "0i"); assert!(format!("{:b}", 0i) == "0"); assert!(format!("{:o}", 0i) == "0"); assert!(format!("{:x}", 0i) == "0"); assert!(format!("{:X}", 0i) == "0"); assert!(format!("{}", 0u) == "0"); + assert!(format!("{:?}", 0u) == "0u"); assert!(format!("{:b}", 0u) == "0"); assert!(format!("{:o}", 0u) == "0"); assert!(format!("{:x}", 0u) == "0"); @@ -183,6 +195,12 @@ mod uint { b.iter(|| { format!("{:x}", rng.gen::()); }) } + #[bench] + fn format_show(b: &mut Bencher) { + let mut rng = weak_rng(); + b.iter(|| { format!("{:?}", rng.gen::()); }) + } + #[bench] fn format_base_36(b: &mut Bencher) { let mut rng = weak_rng(); @@ -219,6 +237,12 @@ mod int { b.iter(|| { format!("{:x}", rng.gen::()); }) } + #[bench] + fn format_show(b: &mut Bencher) { + let mut rng = weak_rng(); + b.iter(|| { format!("{:?}", rng.gen::()); }) + } + #[bench] fn format_base_36(b: &mut Bencher) { let mut rng = weak_rng(); diff --git a/src/libcoretest/result.rs b/src/libcoretest/result.rs index 52ea14dd05d..b9403598ec2 100644 --- a/src/libcoretest/result.rs +++ b/src/libcoretest/result.rs @@ -95,10 +95,10 @@ pub fn test_fmt_default() { let ok: Result = Ok(100); let err: Result = Err("Err"); - let s = format!("{}", ok); - assert_eq!(s, "Ok(100)"); - let s = format!("{}", err); - assert_eq!(s, "Err(Err)"); + let s = format!("{:?}", ok); + assert_eq!(s, "Ok(100i)"); + let s = format!("{:?}", err); + assert_eq!(s, "Err(\"Err\")"); } #[test] diff --git a/src/libcoretest/tuple.rs b/src/libcoretest/tuple.rs index c3bc38a6614..62eb9f4ad34 100644 --- a/src/libcoretest/tuple.rs +++ b/src/libcoretest/tuple.rs @@ -59,10 +59,10 @@ fn test_tuple_cmp() { #[test] fn test_show() { - let s = format!("{}", (1i,)); - assert_eq!(s, "(1,)"); - let s = format!("{}", (1i, true)); - assert_eq!(s, "(1, true)"); - let s = format!("{}", (1i, "hi", true)); - assert_eq!(s, "(1, hi, true)"); + let s = format!("{:?}", (1i,)); + assert_eq!(s, "(1i,)"); + let s = format!("{:?}", (1i, true)); + assert_eq!(s, "(1i, true)"); + let s = format!("{:?}", (1i, "hi", true)); + assert_eq!(s, "(1i, \"hi\", true)"); } -- cgit 1.4.1-3-g733a5