Skip to content

Commit

Permalink
std: Rename Show/String to Debug/Display
Browse files Browse the repository at this point in the history
This commit is an implementation of [RFC 565][rfc] which is a stabilization of
the `std::fmt` module and the implementations of various formatting traits.
Specifically, the following changes were performed:

[rfc]: https://github.com/rust-lang/rfcs/blob/master/text/0565-show-string-guidelines.md

* The `Show` trait is now deprecated, it was renamed to `Debug`
* The `String` trait is now deprecated, it was renamed to `Display`
* Many `Debug` and `Display` implementations were audited in accordance with the
  RFC and audited implementations now have the `#[stable]` attribute
  * Integers and floats no longer print a suffix
  * Smart pointers no longer print details that they are a smart pointer
  * Paths with `Debug` are now quoted and escape characters
* The `unwrap` methods on `Result` now require `Display` instead of `Debug`
* The `Error` trait no longer has a `detail` method and now requires that
  `Display` must be implemented. With the loss of `String`, this has moved into
  libcore.
* `impl<E: Error> FromError<E> for Box<Error>` now exists
* `derive(Show)` has been renamed to `derive(Debug)`. This is not currently
  warned about due to warnings being emitted on stage1+

While backwards compatibility is attempted to be maintained with a blanket
implementation of `Display` for the old `String` trait (and the same for
`Show`/`Debug`) this is still a breaking change due to primitives no longer
implementing `String` as well as modifications such as `unwrap` and the `Error`
trait. Most code is fairly straightforward to update with a rename or tweaks of
method calls.

[breaking-change]
Closes #21436
  • Loading branch information
alexcrichton committed Jan 21, 2015
1 parent 29bd9a0 commit 3cb9fa2
Show file tree
Hide file tree
Showing 136 changed files with 763 additions and 706 deletions.
12 changes: 3 additions & 9 deletions src/compiletest/common.rs
Expand Up @@ -13,7 +13,7 @@ use std::fmt;
use std::str::FromStr;
use regex::Regex;

#[derive(Clone, PartialEq)]
#[derive(Clone, PartialEq, Debug)]
pub enum Mode {
CompileFail,
RunFail,
Expand Down Expand Up @@ -43,9 +43,9 @@ impl FromStr for Mode {
}
}

impl fmt::String for Mode {
impl fmt::Display for Mode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(match *self {
fmt::Display::fmt(match *self {
CompileFail => "compile-fail",
RunFail => "run-fail",
RunPass => "run-pass",
Expand All @@ -58,12 +58,6 @@ impl fmt::String for Mode {
}
}

impl fmt::Show for Mode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(self, f)
}
}

#[derive(Clone)]
pub struct Config {
// The library paths required for running the compiler
Expand Down
6 changes: 3 additions & 3 deletions src/compiletest/runtest.rs
Expand Up @@ -547,7 +547,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {

// Add line breakpoints
for line in breakpoint_lines.iter() {
script_str.push_str(&format!("break '{:?}':{}\n",
script_str.push_str(&format!("break '{}':{}\n",
testfile.filename_display(),
*line)[]);
}
Expand Down Expand Up @@ -750,7 +750,7 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testfile: &Path)
status: status,
stdout: out,
stderr: err,
cmdline: format!("{}", cmd)
cmdline: format!("{:?}", cmd)
};
}
}
Expand Down Expand Up @@ -953,7 +953,7 @@ fn check_expected_errors(expected_errors: Vec<errors::ExpectedError> ,
}

let prefixes = expected_errors.iter().map(|ee| {
format!("{:?}:{}:", testfile.display(), ee.line)
format!("{}:{}:", testfile.display(), ee.line)
}).collect::<Vec<String> >();

#[cfg(windows)]
Expand Down
13 changes: 7 additions & 6 deletions src/liballoc/arc.rs
Expand Up @@ -72,7 +72,7 @@ use core::prelude::*;
use core::atomic;
use core::atomic::Ordering::{Relaxed, Release, Acquire, SeqCst};
use core::borrow::BorrowFrom;
use core::fmt::{self, Show};
use core::fmt;
use core::cmp::{Ordering};
use core::default::Default;
use core::mem::{min_align_of, size_of};
Expand Down Expand Up @@ -578,16 +578,17 @@ impl<T: Ord> Ord for Arc<T> {
#[stable]
impl<T: Eq> Eq for Arc<T> {}

impl<T: fmt::Show> fmt::Show for Arc<T> {
#[stable]
impl<T: fmt::Display> fmt::Display for Arc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Arc({:?})", (**self))
fmt::Display::fmt(&**self, f)
}
}

#[stable]
impl<T: fmt::String> fmt::String for Arc<T> {
impl<T: fmt::Debug> fmt::Debug for Arc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(&**self, f)
fmt::Debug::fmt(&**self, f)
}
}

Expand Down Expand Up @@ -806,7 +807,7 @@ mod tests {
#[test]
fn show_arc() {
let a = Arc::new(5u32);
assert!(format!("{:?}", a) == "Arc(5u32)")
assert_eq!(format!("{:?}", a), "5");
}

// Make sure deriving works with Arc<T>
Expand Down
23 changes: 16 additions & 7 deletions src/liballoc/boxed.rs
Expand Up @@ -16,16 +16,17 @@ use core::any::Any;
use core::clone::Clone;
use core::cmp::{PartialEq, PartialOrd, Eq, Ord, Ordering};
use core::default::Default;
use core::error::{Error, FromError};
use core::fmt;
use core::hash::{self, Hash};
use core::marker::Sized;
use core::mem;
use core::ops::{Deref, DerefMut};
use core::option::Option;
use core::ptr::Unique;
use core::raw::TraitObject;
use core::result::Result;
use core::result::Result::{Ok, Err};
use core::ops::{Deref, DerefMut};
use core::result::Result;

/// A value that represents the global exchange heap. This is the default
/// place that the `box` keyword allocates into when no place is supplied.
Expand Down Expand Up @@ -156,20 +157,22 @@ impl BoxAny for Box<Any> {
}
}

impl<T: ?Sized + fmt::Show> fmt::Show for Box<T> {
#[stable]
impl<T: fmt::Display + ?Sized> fmt::Display for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Box({:?})", &**self)
fmt::Display::fmt(&**self, f)
}
}

#[stable]
impl<T: ?Sized + fmt::String> fmt::String for Box<T> {
impl<T: fmt::Debug + ?Sized> fmt::Debug for Box<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(&**self, f)
fmt::Debug::fmt(&**self, f)
}
}

impl fmt::Show for Box<Any> {
#[stable]
impl fmt::Debug for Box<Any> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("Box<Any>")
}
Expand All @@ -187,6 +190,12 @@ impl<T: ?Sized> DerefMut for Box<T> {
fn deref_mut(&mut self) -> &mut T { &mut **self }
}

impl<'a, E: Error + 'a> FromError<E> for Box<Error + 'a> {
fn from_error(err: E) -> Box<Error + 'a> {
Box::new(err)
}
}

#[cfg(test)]
mod test {
#[test]
Expand Down
16 changes: 8 additions & 8 deletions src/liballoc/rc.rs
Expand Up @@ -693,17 +693,17 @@ impl<S: hash::Hasher, T: Hash<S>> Hash<S> for Rc<T> {
}
}

#[unstable = "Show is experimental."]
impl<T: fmt::Show> fmt::Show for Rc<T> {
#[stable]
impl<T: fmt::Display> fmt::Display for Rc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Rc({:?})", **self)
fmt::Display::fmt(&**self, f)
}
}

#[stable]
impl<T: fmt::String> fmt::String for Rc<T> {
impl<T: fmt::Debug> fmt::Debug for Rc<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::String::fmt(&**self, f)
fmt::Debug::fmt(&**self, f)
}
}

Expand Down Expand Up @@ -890,8 +890,8 @@ impl<T> Clone for Weak<T> {
}
}

#[unstable = "Show is experimental."]
impl<T: fmt::Show> fmt::Show for Weak<T> {
#[stable]
impl<T: fmt::Debug> fmt::Debug for Weak<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(Weak)")
}
Expand Down Expand Up @@ -1134,7 +1134,7 @@ mod tests {
#[test]
fn test_show() {
let foo = Rc::new(75u);
assert!(format!("{:?}", foo) == "Rc(75u)")
assert_eq!(format!("{:?}", foo), "75");
}

}
6 changes: 3 additions & 3 deletions src/libcollections/bit.rs
Expand Up @@ -972,7 +972,7 @@ impl Ord for Bitv {
}

#[stable]
impl fmt::Show for Bitv {
impl fmt::Debug for Bitv {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
for bit in self.iter() {
try!(write!(fmt, "{}", if bit { 1u32 } else { 0u32 }));
Expand Down Expand Up @@ -1727,7 +1727,7 @@ impl BitvSet {
}
}

impl fmt::Show for BitvSet {
impl fmt::Debug for BitvSet {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "BitvSet {{"));
let mut first = true;
Expand Down Expand Up @@ -2622,7 +2622,7 @@ mod bitv_set_test {
s.insert(10);
s.insert(50);
s.insert(2);
assert_eq!("BitvSet {1u, 2u, 10u, 50u}", format!("{:?}", s));
assert_eq!("BitvSet {1, 2, 10, 50}", format!("{:?}", s));
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/libcollections/btree/map.rs
Expand Up @@ -22,7 +22,7 @@ use core::prelude::*;
use core::borrow::BorrowFrom;
use core::cmp::Ordering;
use core::default::Default;
use core::fmt::Show;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::iter::{Map, FromIterator};
use core::ops::{Index, IndexMut};
Expand Down Expand Up @@ -871,7 +871,7 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
}

#[stable]
impl<K: Show, V: Show> Show for BTreeMap<K, V> {
impl<K: Debug, V: Debug> Debug for BTreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "BTreeMap {{"));

Expand Down
6 changes: 3 additions & 3 deletions src/libcollections/btree/set.rs
Expand Up @@ -16,7 +16,7 @@ use core::prelude::*;
use core::borrow::BorrowFrom;
use core::cmp::Ordering::{self, Less, Greater, Equal};
use core::default::Default;
use core::fmt::Show;
use core::fmt::Debug;
use core::fmt;
// NOTE(stage0) remove import after a snapshot
#[cfg(stage0)]
Expand Down Expand Up @@ -592,7 +592,7 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>> for &'a BTreeSet<T> {
}

#[stable]
impl<T: Show> Show for BTreeSet<T> {
impl<T: Debug> Debug for BTreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "BTreeSet {{"));

Expand Down Expand Up @@ -892,7 +892,7 @@ mod test {

let set_str = format!("{:?}", set);

assert_eq!(set_str, "BTreeSet {1i, 2i}");
assert_eq!(set_str, "BTreeSet {1, 2}");
assert_eq!(format!("{:?}", empty), "BTreeSet {}");
}
}
4 changes: 2 additions & 2 deletions src/libcollections/dlist.rs
Expand Up @@ -874,7 +874,7 @@ impl<A: Clone> Clone for DList<A> {
}

#[stable]
impl<A: fmt::Show> fmt::Show for DList<A> {
impl<A: fmt::Debug> fmt::Debug for DList<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "DList ["));

Expand Down Expand Up @@ -1333,7 +1333,7 @@ mod tests {
#[test]
fn test_show() {
let list: DList<int> = range(0i, 10).collect();
assert_eq!(format!("{:?}", list), "DList [0i, 1i, 2i, 3i, 4i, 5i, 6i, 7i, 8i, 9i]");
assert_eq!(format!("{:?}", list), "DList [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");

let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/enum_set.rs
Expand Up @@ -31,7 +31,7 @@ pub struct EnumSet<E> {

impl<E> Copy for EnumSet<E> {}

impl<E:CLike+fmt::Show> fmt::Show for EnumSet<E> {
impl<E:CLike + fmt::Debug> fmt::Debug for EnumSet<E> {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "EnumSet {{"));
let mut first = true;
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/ring_buf.rs
Expand Up @@ -1611,7 +1611,7 @@ impl<A> Extend<A> for RingBuf<A> {
}

#[stable]
impl<T: fmt::Show> fmt::Show for RingBuf<T> {
impl<T: fmt::Debug> fmt::Debug for RingBuf<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "RingBuf ["));

Expand All @@ -1630,7 +1630,7 @@ mod tests {
use self::Taggypar::*;
use prelude::*;
use core::iter;
use std::fmt::Show;
use std::fmt::Debug;
use std::hash::{self, SipHasher};
use test::Bencher;
use test;
Expand Down Expand Up @@ -1678,7 +1678,7 @@ mod tests {
}

#[cfg(test)]
fn test_parameterized<T:Clone + PartialEq + Show>(a: T, b: T, c: T, d: T) {
fn test_parameterized<T:Clone + PartialEq + Debug>(a: T, b: T, c: T, d: T) {
let mut deq = RingBuf::new();
assert_eq!(deq.len(), 0);
deq.push_front(a.clone());
Expand Down Expand Up @@ -2302,7 +2302,7 @@ mod tests {
#[test]
fn test_show() {
let ringbuf: RingBuf<int> = range(0i, 10).collect();
assert_eq!(format!("{:?}", ringbuf), "RingBuf [0i, 1i, 2i, 3i, 4i, 5i, 6i, 7i, 8i, 9i]");
assert_eq!(format!("{:?}", ringbuf), "RingBuf [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");

let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
Expand Down
12 changes: 6 additions & 6 deletions src/libcollections/slice.rs
Expand Up @@ -2476,19 +2476,19 @@ mod tests {
}
let empty: Vec<int> = vec![];
test_show_vec!(empty, "[]");
test_show_vec!(vec![1i], "[1i]");
test_show_vec!(vec![1i, 2, 3], "[1i, 2i, 3i]");
test_show_vec!(vec![1i], "[1]");
test_show_vec!(vec![1i, 2, 3], "[1, 2, 3]");
test_show_vec!(vec![vec![], vec![1u], vec![1u, 1u]],
"[[], [1u], [1u, 1u]]");
"[[], [1], [1, 1]]");

let empty_mut: &mut [int] = &mut[];
test_show_vec!(empty_mut, "[]");
let v: &mut[int] = &mut[1];
test_show_vec!(v, "[1i]");
test_show_vec!(v, "[1]");
let v: &mut[int] = &mut[1, 2, 3];
test_show_vec!(v, "[1i, 2i, 3i]");
test_show_vec!(v, "[1, 2, 3]");
let v: &mut [&mut[uint]] = &mut[&mut[], &mut[1u], &mut[1u, 1u]];
test_show_vec!(v, "[[], [1u], [1u, 1u]]");
test_show_vec!(v, "[[], [1], [1, 1]]");
}

#[test]
Expand Down

0 comments on commit 3cb9fa2

Please sign in to comment.