Skip to content

Commit 9731c28

Browse files
committed
Implement Show for SmallIntMap
1 parent f1bff59 commit 9731c28

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/libcollections/smallintmap.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
use core::prelude::*;
1919

20+
use core::fmt;
2021
use core::iter::{Enumerate, FilterMap};
2122
use core::mem::replace;
2223

@@ -176,6 +177,18 @@ impl<V:Clone> SmallIntMap<V> {
176177
}
177178
}
178179

180+
impl<V: fmt::Show> fmt::Show for SmallIntMap<V> {
181+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
182+
try!(write!(f, r"\{"));
183+
184+
for (i, (k, v)) in self.iter().enumerate() {
185+
if i != 0 { try!(write!(f, ", ")); }
186+
try!(write!(f, "{}: {}", k, *v));
187+
}
188+
189+
write!(f, r"\}")
190+
}
191+
}
179192

180193
macro_rules! iterator {
181194
(impl $name:ident -> $elem:ty, $getter:ident) => {
@@ -461,6 +474,20 @@ mod test_map {
461474
assert!(called);
462475
m.insert(2, box 1);
463476
}
477+
478+
#[test]
479+
fn test_show() {
480+
let mut map = SmallIntMap::new();
481+
let empty = SmallIntMap::<int>::new();
482+
483+
map.insert(1, 2);
484+
map.insert(3, 4);
485+
486+
let map_str = map.to_str();
487+
let map_str = map_str.as_slice();
488+
assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
489+
assert_eq!(format!("{}", empty), "{}".to_string());
490+
}
464491
}
465492

466493
#[cfg(test)]

0 commit comments

Comments
 (0)