File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1717
1818use core:: prelude:: * ;
1919
20+ use core:: fmt;
2021use core:: iter:: { Enumerate , FilterMap } ;
2122use 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
180193macro_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) ]
You can’t perform that action at this time.
0 commit comments