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/liballoc/arc.rs | 4 ++-- src/liballoc/boxed.rs | 8 +++++++- src/liballoc/rc.rs | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 25f80ad11bd..7789620a64f 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -581,7 +581,7 @@ impl Eq for Arc {} impl fmt::Show for Arc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (**self).fmt(f) + write!(f, "Arc({:?})", (**self)) } } @@ -794,7 +794,7 @@ mod tests { #[test] fn show_arc() { let a = Arc::new(5u32); - assert!(format!("{}", a) == "5") + assert!(format!("{:?}", a) == "Arc(5u32)") } // Make sure deriving works with Arc diff --git a/src/liballoc/boxed.rs b/src/liballoc/boxed.rs index 6df8bb5f7aa..b4eb71df167 100644 --- a/src/liballoc/boxed.rs +++ b/src/liballoc/boxed.rs @@ -145,7 +145,13 @@ impl BoxAny for Box { impl fmt::Show for Box { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (**self).fmt(f) + write!(f, "Box({:?})", &**self) + } +} + +impl fmt::String for Box { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fmt::String::fmt(&**self, f) } } diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 175bba4e71d..91566e89a16 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -607,7 +607,7 @@ impl> Hash for Rc { #[experimental = "Show is experimental."] impl fmt::Show for Rc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (**self).fmt(f) + write!(f, "Rc({:?})", **self) } } @@ -962,4 +962,10 @@ mod tests { assert!(cow1_weak.upgrade().is_none()); } + #[test] + fn test_show() { + let foo = Rc::new(75u); + assert!(format!("{:?}", foo) == "Rc(75u)") + } + } -- cgit 1.4.1-3-g733a5