Skip to content

Commit 6720977

Browse files
committed
auto merge of #12412 : alexcrichton/rust/deriving-show, r=huonw
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
2 parents a5342d5 + 8761f79 commit 6720977

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+414
-739
lines changed

src/doc/tutorial.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2519,7 +2519,7 @@ of type `ABC` can be randomly generated and converted to a string:
25192519
#[deriving(Eq)]
25202520
struct Circle { radius: f64 }
25212521
2522-
#[deriving(Rand, ToStr)]
2522+
#[deriving(Rand, Show)]
25232523
enum ABC { A, B, C }
25242524
~~~
25252525

src/libcollections/btree.rs

+32-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
///a length (the height of the tree), and lower and upper bounds on the
1919
///number of elements that a given node can contain.
2020
21-
use std::vec::OwnedVector;
21+
use std::fmt;
22+
use std::fmt::Show;
2223

2324
#[allow(missing_doc)]
2425
pub struct BTree<K, V> {
@@ -106,11 +107,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BTree<K, V> {
106107
}
107108
}
108109

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

@@ -235,15 +235,15 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Node<K, V> {
235235
}
236236
}
237237

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

403403

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

@@ -618,13 +622,14 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for Branch<K, V> {
618622
}
619623
}
620624

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

@@ -672,11 +677,10 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for LeafElt<K, V> {
672677
}
673678
}
674679

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

@@ -715,12 +719,12 @@ impl<K: TotalOrd, V: TotalEq> TotalOrd for BranchElt<K, V> {
715719
}
716720
}
717721

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

src/libcollections/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
1616
use std::num::Bitwise;
1717

18-
#[deriving(Clone, Eq, Hash, ToStr, Encodable, Decodable)]
18+
#[deriving(Clone, Eq, Hash, Show, Encodable, Decodable)]
1919
/// A specialized Set implementation to use enum types.
2020
pub struct EnumSet<E> {
2121
// We must maintain the invariant that no bits are set

src/libcollections/hashmap.rs

-8
Original file line numberDiff line numberDiff line change
@@ -606,10 +606,6 @@ impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
606606
}
607607
}
608608

609-
impl<K: fmt::Show + Hash + Eq, V: fmt::Show> ToStr for HashMap<K, V> {
610-
fn to_str(&self) -> ~str { format!("{}", *self) }
611-
}
612-
613609
/// HashMap iterator
614610
#[deriving(Clone)]
615611
pub struct Entries<'a, K, V> {
@@ -888,10 +884,6 @@ impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
888884
}
889885
}
890886

891-
impl<A: fmt::Show + Hash + Eq> ToStr for HashSet<A> {
892-
fn to_str(&self) -> ~str { format!("{}", *self) }
893-
}
894-
895887
impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
896888
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
897889
let (lower, _) = iter.size_hint();

src/libcollections/lru_cache.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
//! assert!(cache.get(&2).is_none());
3838
//! ```
3939
40+
use std::cast;
4041
use std::container::Container;
4142
use std::hash::{Hash, sip};
43+
use std::fmt;
4244
use std::ptr;
43-
use std::cast;
4445

4546
use HashMap;
4647

@@ -217,36 +218,32 @@ impl<K: Hash + Eq, V> LruCache<K, V> {
217218
}
218219
}
219220

220-
impl<A: ToStr + Hash + Eq, B: ToStr> ToStr for LruCache<A, B> {
221+
impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for LruCache<A, B> {
221222
/// Return a string that lists the key-value pairs from most-recently
222223
/// used to least-recently used.
223-
#[inline]
224-
fn to_str(&self) -> ~str {
225-
let mut acc = ~"{";
224+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
225+
try!(write!(f.buf, r"\{"));
226226
let mut cur = self.head;
227227
for i in range(0, self.len()) {
228-
if i > 0 {
229-
acc.push_str(", ");
230-
}
228+
if i > 0 { try!(write!(f.buf, ", ")) }
231229
unsafe {
232230
cur = (*cur).next;
233231
match (*cur).key {
234232
// should never print nil
235-
None => acc.push_str("nil"),
236-
Some(ref k) => acc.push_str(k.to_str())
233+
None => try!(write!(f.buf, "nil")),
234+
Some(ref k) => try!(write!(f.buf, "{}", *k)),
237235
}
238236
}
239-
acc.push_str(": ");
237+
try!(write!(f.buf, ": "));
240238
unsafe {
241239
match (*cur).value {
242240
// should never print nil
243-
None => acc.push_str("nil"),
244-
Some(ref value) => acc.push_str(value.to_str())
241+
None => try!(write!(f.buf, "nil")),
242+
Some(ref value) => try!(write!(f.buf, "{}", *value)),
245243
}
246244
}
247245
}
248-
acc.push_char('}');
249-
acc
246+
write!(f.buf, r"\}")
250247
}
251248
}
252249

src/libextra/json.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ use std::io;
240240
use std::io::MemWriter;
241241
use std::num;
242242
use std::str;
243-
use std::to_str;
243+
use std::fmt;
244244

245245
use serialize::Encodable;
246246
use serialize;
@@ -1576,18 +1576,16 @@ impl<A:ToJson> ToJson for Option<A> {
15761576
}
15771577
}
15781578

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

1588-
impl to_str::ToStr for Error {
1589-
fn to_str(&self) -> ~str {
1590-
format!("{}:{}: {}", self.line, self.col, self.msg)
1586+
impl fmt::Show for Error {
1587+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1588+
write!(f.buf, "{}:{}: {}", self.line, self.col, self.msg)
15911589
}
15921590
}
15931591

0 commit comments

Comments
 (0)