auto merge of #14447 : erickt/rust/show-treemap, r=kballard

This is a hodge podge of a couple small cleanup commits. It implements `Show` for `TreeMap` and `TreeSet`, and some removal of commented out code.
This commit is contained in:
bors 2014-05-27 16:11:39 -07:00
commit 30bf73fd78
3 changed files with 72 additions and 32 deletions

View File

@ -2003,6 +2003,20 @@ fn test_eq() {
assert_eq!(m1, m2);
}
#[test]
fn test_show() {
let mut map: HashMap<int, int> = HashMap::new();
let empty: HashMap<int, int> = HashMap::new();
map.insert(1, 2);
map.insert(3, 4);
let map_str = format!("{}", map);
assert!(map_str == "{1: 2, 3: 4}".to_owned() || map_str == "{3: 4, 1: 2}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}
#[test]
fn test_expand() {
let mut m = HashMap::new();

View File

@ -12,9 +12,11 @@
//! trees. The only requirement for the types is that the key implements
//! `TotalOrd`.
use std::iter;
use std::iter::{Peekable};
use std::cmp::Ordering;
use std::fmt::Show;
use std::fmt;
use std::iter::Peekable;
use std::iter;
use std::mem::{replace, swap};
use std::ptr;
@ -67,6 +69,19 @@ impl<K: Ord + TotalOrd, V: Ord> Ord for TreeMap<K, V> {
fn lt(&self, other: &TreeMap<K, V>) -> bool { lt(self, other) }
}
impl<K: TotalOrd + Show, V: Show> Show for TreeMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));
for (i, (k, v)) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}: {}", *k, *v));
}
write!(f, r"\}")
}
}
impl<K: TotalOrd, V> Container for TreeMap<K, V> {
fn len(&self) -> uint { self.length }
}
@ -547,6 +562,19 @@ impl<T: Ord + TotalOrd> Ord for TreeSet<T> {
fn lt(&self, other: &TreeSet<T>) -> bool { self.map < other.map }
}
impl<T: TotalOrd + Show> Show for TreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, r"\{"));
for (i, x) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *x));
}
write!(f, r"\}")
}
}
impl<T: TotalOrd> Container for TreeSet<T> {
#[inline]
fn len(&self) -> uint { self.map.len() }
@ -1328,6 +1356,20 @@ fn test_ord() {
assert!(a < b && a <= b);
}
#[test]
fn test_show() {
let mut map: TreeMap<int, int> = TreeMap::new();
let empty: TreeMap<int, int> = TreeMap::new();
map.insert(1, 2);
map.insert(3, 4);
let map_str = format!("{}", map);
assert!(map_str == "{1: 2, 3: 4}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}
#[test]
fn test_lazy_iterator() {
let mut m = TreeMap::new();
@ -1723,4 +1765,18 @@ fn test_from_iter() {
assert!(set.contains(x));
}
}
#[test]
fn test_show() {
let mut set: TreeSet<int> = TreeSet::new();
let empty: TreeSet<int> = TreeSet::new();
set.insert(1);
set.insert(2);
let set_str = format!("{}", set);
assert!(set_str == "{1, 2}".to_owned());
assert_eq!(format!("{}", empty), "{}".to_owned());
}
}

View File

@ -1434,36 +1434,6 @@ fn parse_str(&mut self) -> Result<String, ParserError> {
},
},
_ => return self.error(InvalidEscape),
/*=======
'u' => {
// Parse \u1234.
let mut i = 0u;
let mut n = 0u;
while i < 4u && !self.eof() {
self.bump();
n = match self.ch_or_null() {
c @ '0' .. '9' => n * 16u + (c as uint) - ('0' as uint),
'a' | 'A' => n * 16u + 10u,
'b' | 'B' => n * 16u + 11u,
'c' | 'C' => n * 16u + 12u,
'd' | 'D' => n * 16u + 13u,
'e' | 'E' => n * 16u + 14u,
'f' | 'F' => n * 16u + 15u,
_ => return self.error(UnrecognizedHex)
};
i += 1u;
}
// Error out if we didn't parse 4 digits.
if i != 4u {
return self.error(NotFourDigit);
}
res.push_char(char::from_u32(n as u32).unwrap());
}
_ => return self.error(InvalidEscape),
>>>>>>> Add a streaming parser to serialize::json.*/
}
escape = false;
} else if self.ch_is('\\') {