Skip to content

Commit

Permalink
auto merge of #12412 : alexcrichton/rust/deriving-show, r=huonw
Browse files Browse the repository at this point in the history
This commit removes deriving(ToStr) in favor of deriving(Show), migrating all impls of ToStr to fmt::Show.

Most of the details can be found in the first commit message.

Closes #12477
  • Loading branch information
bors committed Feb 24, 2014
2 parents a5342d5 + 8761f79 commit 6720977
Show file tree
Hide file tree
Showing 58 changed files with 414 additions and 739 deletions.
2 changes: 1 addition & 1 deletion src/doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ of type `ABC` can be randomly generated and converted to a string:
#[deriving(Eq)]
struct Circle { radius: f64 }
#[deriving(Rand, ToStr)]
#[deriving(Rand, Show)]
enum ABC { A, B, C }
~~~

Expand Down
60 changes: 32 additions & 28 deletions src/libcollections/btree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
///a length (the height of the tree), and lower and upper bounds on the
///number of elements that a given node can contain.

use std::vec::OwnedVector;
use std::fmt;
use std::fmt::Show;

#[allow(missing_doc)]
pub struct BTree<K, V> {
Expand Down Expand Up @@ -106,11 +107,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BTree<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BTree<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BTree<K, V> {
///Returns a string representation of the BTree
fn to_str(&self) -> ~str {
let ret = self.root.to_str();
ret
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.root.fmt(f)
}
}

Expand Down Expand Up @@ -235,15 +235,15 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Node<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Node<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Node<K, V> {
///Returns a string representation of a Node.
///Will iterate over the Node and show "Key: x, value: y, child: () // "
///for all elements in the Node. "Child" only exists if the Node contains
///a branch.
fn to_str(&self) -> ~str {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
LeafNode(ref leaf) => leaf.to_str(),
BranchNode(ref branch) => branch.to_str()
LeafNode(ref leaf) => leaf.fmt(f),
BranchNode(ref branch) => branch.fmt(f),
}
}
}
Expand Down Expand Up @@ -401,10 +401,14 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Leaf<K, V> {
}


impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Leaf<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Leaf<K, V> {
///Returns a string representation of a Leaf.
fn to_str(&self) -> ~str {
self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // ")
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
}
Ok(())
}
}

Expand Down Expand Up @@ -618,13 +622,14 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for Branch<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for Branch<K, V> {
///Returns a string representation of a Branch.
fn to_str(&self) -> ~str {
let mut ret = self.elts.iter().map(|s| s.to_str()).to_owned_vec().connect(" // ");
ret.push_str(" // ");
ret.push_str("rightmost child: ("+ self.rightmost_child.to_str() +") ");
ret
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, s) in self.elts.iter().enumerate() {
if i != 0 { try!(write!(f.buf, " // ")) }
try!(write!(f.buf, "{}", *s))
}
write!(f.buf, " // rightmost child: ({}) ", *self.rightmost_child)
}
}

Expand Down Expand Up @@ -672,11 +677,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for LeafElt<K, V> {
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for LeafElt<K, V> {
///Returns a string representation of a LeafElt.
fn to_str(&self) -> ~str {
format!("Key: {}, value: {};",
self.key.to_str(), self.value.to_str())
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {};", self.key, self.value)
}
}

Expand Down Expand Up @@ -715,12 +719,12 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BranchElt<K, V> {
}
}

impl<K: ToStr + TotalOrd, V: ToStr> ToStr for BranchElt<K, V> {
///Returns string containing key, value, and child (which should recur to a leaf)
///Consider changing in future to be more readable.
fn to_str(&self) -> ~str {
format!("Key: {}, value: {}, (child: {})",
self.key.to_str(), self.value.to_str(), self.left.to_str())
impl<K: fmt::Show + TotalOrd, V: fmt::Show> fmt::Show for BranchElt<K, V> {
/// Returns string containing key, value, and child (which should recur to a
/// leaf) Consider changing in future to be more readable.
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "Key: {}, value: {}, (child: {})",
self.key, self.value, *self.left)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use std::num::Bitwise;

#[deriving(Clone, Eq, Hash, ToStr, Encodable, Decodable)]
#[deriving(Clone, Eq, Hash, Show, Encodable, Decodable)]
/// A specialized Set implementation to use enum types.
pub struct EnumSet<E> {
// We must maintain the invariant that no bits are set
Expand Down
8 changes: 0 additions & 8 deletions src/libcollections/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,6 @@ impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
}
}

impl<K: fmt::Show + Hash + Eq, V: fmt::Show> ToStr for HashMap<K, V> {
fn to_str(&self) -> ~str { format!("{}", *self) }
}

/// HashMap iterator
#[deriving(Clone)]
pub struct Entries<'a, K, V> {
Expand Down Expand Up @@ -888,10 +884,6 @@ impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
}
}

impl<A: fmt::Show + Hash + Eq> ToStr for HashSet<A> {
fn to_str(&self) -> ~str { format!("{}", *self) }
}

impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
let (lower, _) = iter.size_hint();
Expand Down
27 changes: 12 additions & 15 deletions src/libcollections/lru_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
//! assert!(cache.get(&2).is_none());
//! ```

use std::cast;
use std::container::Container;
use std::hash::{Hash, sip};
use std::fmt;
use std::ptr;
use std::cast;

use HashMap;

Expand Down Expand Up @@ -217,36 +218,32 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
}
}

impl<A: ToStr + Hash + Eq, B: ToStr> ToStr for LruCache<A, B> {
impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for LruCache<A, B> {
/// Return a string that lists the key-value pairs from most-recently
/// used to least-recently used.
#[inline]
fn to_str(&self) -> ~str {
let mut acc = ~"{";
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f.buf, r"\{"));
let mut cur = self.head;
for i in range(0, self.len()) {
if i > 0 {
acc.push_str(", ");
}
if i > 0 { try!(write!(f.buf, ", ")) }
unsafe {
cur = (*cur).next;
match (*cur).key {
// should never print nil
None => acc.push_str("nil"),
Some(ref k) => acc.push_str(k.to_str())
None => try!(write!(f.buf, "nil")),
Some(ref k) => try!(write!(f.buf, "{}", *k)),
}
}
acc.push_str(": ");
try!(write!(f.buf, ": "));
unsafe {
match (*cur).value {
// should never print nil
None => acc.push_str("nil"),
Some(ref value) => acc.push_str(value.to_str())
None => try!(write!(f.buf, "nil")),
Some(ref value) => try!(write!(f.buf, "{}", *value)),
}
}
}
acc.push_char('}');
acc
write!(f.buf, r"\}")
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/libextra/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ use std::io;
use std::io::MemWriter;
use std::num;
use std::str;
use std::to_str;
use std::fmt;

use serialize::Encodable;
use serialize;
Expand Down Expand Up @@ -1576,18 +1576,16 @@ impl<A:ToJson> ToJson for Option<A> {
}
}

impl to_str::ToStr for Json {
impl fmt::Show for Json {
/// Encodes a json value into a string
fn to_str(&self) -> ~str {
let mut s = MemWriter::new();
self.to_writer(&mut s as &mut io::Writer).unwrap();
str::from_utf8_owned(s.unwrap()).unwrap()
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_writer(f.buf)
}
}

impl to_str::ToStr for Error {
fn to_str(&self) -> ~str {
format!("{}:{}: {}", self.line, self.col, self.msg)
impl fmt::Show for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f.buf, "{}:{}: {}", self.line, self.col, self.msg)
}
}

Expand Down
Loading

0 comments on commit 6720977

Please sign in to comment.