Skip to content

Commit cdd46f8

Browse files
committed
auto merge of #17245 : sfackler/rust/enumset-show, r=alexcrichton
2 parents c09437a + 0e8cc52 commit cdd46f8

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/libcollections/enum_set.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,31 @@
1414
//! representation to hold C-like enum variants.
1515
1616
use core::prelude::*;
17+
use core::fmt;
1718

18-
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
19+
#[deriving(Clone, PartialEq, Eq, Hash)]
1920
/// A specialized `Set` implementation to use enum types.
2021
pub struct EnumSet<E> {
2122
// We must maintain the invariant that no bits are set
2223
// for which no variant exists
2324
bits: uint
2425
}
2526

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+
2642
/// An interface for casting C-like enum to uint and back.
2743
pub trait CLike {
2844
/// Converts a C-like enum to a `uint`.
@@ -165,6 +181,16 @@ mod test {
165181
assert!(e.is_empty());
166182
}
167183

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+
168194
///////////////////////////////////////////////////////////////////////////
169195
// intersect
170196

0 commit comments

Comments
 (0)