提交 9731c28e 编写于 作者: A Adolfo Ochagavía

Implement Show for SmallIntMap

上级 f1bff592
......@@ -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)]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册