auto merge of #14725 : aochagavia/rust/show, r=alexcrichton

This commit is contained in:
bors 2014-06-07 15:31:44 -07:00
commit fa1832800a
2 changed files with 52 additions and 0 deletions

View File

@ -24,6 +24,7 @@
use core::prelude::*;
use alloc::owned::Box;
use core::fmt;
use core::iter;
use core::mem;
use core::ptr;
@ -608,6 +609,19 @@ fn clone(&self) -> DList<A> {
}
}
impl<A: fmt::Show> fmt::Show for DList<A> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "["));
for (i, e) in self.iter().enumerate() {
if i != 0 { try!(write!(f, ", ")); }
try!(write!(f, "{}", *e));
}
write!(f, "]")
}
}
#[cfg(test)]
mod tests {
use std::prelude::*;
@ -1027,6 +1041,17 @@ fn test_fuzz() {
}
}
#[test]
fn test_show() {
let list: DList<int> = range(0, 10).collect();
assert!(list.to_str().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
.map(|&s| s)
.collect();
assert!(list.to_str().as_slice() == "[just, one, test, more]");
}
#[cfg(test)]
fn fuzz_test(sz: int) {
let mut m: DList<int> = DList::new();

View File

@ -17,6 +17,7 @@
use core::prelude::*;
use core::fmt;
use core::iter::{Enumerate, FilterMap};
use core::mem::replace;
@ -176,6 +177,18 @@ pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool {
}
}
impl<V: fmt::Show> fmt::Show for SmallIntMap<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"\}")
}
}
macro_rules! iterator {
(impl $name:ident -> $elem:ty, $getter:ident) => {
@ -461,6 +474,20 @@ fn test_move_iter() {
assert!(called);
m.insert(2, box 1);
}
#[test]
fn test_show() {
let mut map = SmallIntMap::new();
let empty = SmallIntMap::<int>::new();
map.insert(1, 2);
map.insert(3, 4);
let map_str = map.to_str();
let map_str = map_str.as_slice();
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
assert_eq!(format!("{}", empty), "{}".to_string());
}
}
#[cfg(test)]