diff options
| author | Adolfo OchagavĂa <aochagavia92@gmail.com> | 2014-06-07 18:17:58 +0200 |
|---|---|---|
| committer | Adolfo OchagavĂa <aochagavia92@gmail.com> | 2014-06-07 19:44:45 +0200 |
| commit | 9731c28e116943220b397d3131273344f0750913 (patch) | |
| tree | 7aeb9051d11470f32268a40de1fbd81ede9432e6 | |
| parent | f1bff592b1d3dff14459374af930ae3fee6253ce (diff) | |
| download | rust-9731c28e116943220b397d3131273344f0750913.tar.gz rust-9731c28e116943220b397d3131273344f0750913.zip | |
Implement Show for SmallIntMap
| -rw-r--r-- | src/libcollections/smallintmap.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index f3118181bdc..45584dd4b28 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -17,6 +17,7 @@ use core::prelude::*; +use core::fmt; use core::iter::{Enumerate, FilterMap}; use core::mem::replace; @@ -176,6 +177,18 @@ impl<V:Clone> SmallIntMap<V> { } } +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 @@ mod test_map { 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)] |
