|
14 | 14 | //! representation to hold C-like enum variants.
|
15 | 15 |
|
16 | 16 | use core::prelude::*;
|
| 17 | +use core::fmt; |
17 | 18 |
|
18 |
| -#[deriving(Clone, PartialEq, Eq, Hash, Show)] |
| 19 | +#[deriving(Clone, PartialEq, Eq, Hash)] |
19 | 20 | /// A specialized `Set` implementation to use enum types.
|
20 | 21 | pub struct EnumSet<E> {
|
21 | 22 | // We must maintain the invariant that no bits are set
|
22 | 23 | // for which no variant exists
|
23 | 24 | bits: uint
|
24 | 25 | }
|
25 | 26 |
|
| 27 | +impl<E:CLike+fmt::Show> fmt::Show for EnumSet<E> { |
| 28 | + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 29 | + try!(write!(fmt, "{{")); |
| 30 | + let mut first = true; |
| 31 | + for e in self.iter() { |
| 32 | + if !first { |
| 33 | + try!(write!(fmt, ", ")); |
| 34 | + } |
| 35 | + try!(write!(fmt, "{}", e)); |
| 36 | + first = false; |
| 37 | + } |
| 38 | + write!(fmt, "}}") |
| 39 | + } |
| 40 | +} |
| 41 | + |
26 | 42 | /// An interface for casting C-like enum to uint and back.
|
27 | 43 | pub trait CLike {
|
28 | 44 | /// Converts a C-like enum to a `uint`.
|
@@ -165,6 +181,16 @@ mod test {
|
165 | 181 | assert!(e.is_empty());
|
166 | 182 | }
|
167 | 183 |
|
| 184 | + #[test] |
| 185 | + fn test_show() { |
| 186 | + let mut e = EnumSet::empty(); |
| 187 | + assert_eq!("{}", e.to_string().as_slice()); |
| 188 | + e.add(A); |
| 189 | + assert_eq!("{A}", e.to_string().as_slice()); |
| 190 | + e.add(C); |
| 191 | + assert_eq!("{A, C}", e.to_string().as_slice()); |
| 192 | + } |
| 193 | + |
168 | 194 | ///////////////////////////////////////////////////////////////////////////
|
169 | 195 | // intersect
|
170 | 196 |
|
|
0 commit comments