summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorSean McArthur <sean.monstar@gmail.com>2014-12-20 00:09:35 -0800
committerSean McArthur <sean.monstar@gmail.com>2015-01-06 14:49:42 -0800
commit44440e5c18a1dbcc9685866ffffe00c508929079 (patch)
treeb66c50cd1e471dc0e37b8a0db2cf7f8f28fbac5f /src/libcoretest
parent8efd9901b628d687d11a4d0ccc153553b38ada49 (diff)
downloadrust-44440e5c18a1dbcc9685866ffffe00c508929079.tar.gz
rust-44440e5c18a1dbcc9685866ffffe00c508929079.zip
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]
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/any.rs16
-rw-r--r--src/libcoretest/cell.rs8
-rw-r--r--src/libcoretest/fmt/num.rs24
-rw-r--r--src/libcoretest/result.rs8
-rw-r--r--src/libcoretest/tuple.rs12
5 files changed, 46 insertions, 22 deletions
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::<uint>() {
         Some(&5) => {}
-        x => panic!("Unexpected value {}", x)
+        x => panic!("Unexpected value {:?}", x)
     }
 
     match a.downcast_ref::<Test>() {
         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::<uint>() {
@@ -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::<Test>() {
         None => (),
-        x => panic!("Unexpected value {}", x)
+        x => panic!("Unexpected value {:?}", x)
     }
 
     match b_r.downcast_mut::<Test>() {
         None => (),
-        x => panic!("Unexpected value {}", x)
+        x => panic!("Unexpected value {:?}", x)
     }
 
     match a_r.downcast_mut::<uint>() {
         Some(&612) => {}
-        x => panic!("Unexpected value {}", x)
+        x => panic!("Unexpected value {:?}", x)
     }
 
     match b_r.downcast_mut::<uint>() {
         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");
@@ -184,6 +196,12 @@ mod uint {
     }
 
     #[bench]
+    fn format_show(b: &mut Bencher) {
+        let mut rng = weak_rng();
+        b.iter(|| { format!("{:?}", rng.gen::<uint>()); })
+    }
+
+    #[bench]
     fn format_base_36(b: &mut Bencher) {
         let mut rng = weak_rng();
         b.iter(|| { format!("{}", radix(rng.gen::<uint>(), 36)); })
@@ -220,6 +238,12 @@ mod int {
     }
 
     #[bench]
+    fn format_show(b: &mut Bencher) {
+        let mut rng = weak_rng();
+        b.iter(|| { format!("{:?}", rng.gen::<int>()); })
+    }
+
+    #[bench]
     fn format_base_36(b: &mut Bencher) {
         let mut rng = weak_rng();
         b.iter(|| { format!("{}", radix(rng.gen::<int>(), 36)); })
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<int, &'static str> = Ok(100);
     let err: Result<int, &'static str> = 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)");
 }