diff options
| author | Steven Fackler <sfackler@gmail.com> | 2014-09-13 17:37:03 -0700 |
|---|---|---|
| committer | Steven Fackler <sfackler@gmail.com> | 2014-09-13 17:37:03 -0700 |
| commit | 0e8cc5231154fa0fd54cd173e2dd05a7cce77dc6 (patch) | |
| tree | da7270964fb42c12e55485d48a994003e2305bfe /src/libcollections/enum_set.rs | |
| parent | 79a5448f41dcc6ab52663105a6b02fc5af4c503e (diff) | |
| download | rust-0e8cc5231154fa0fd54cd173e2dd05a7cce77dc6.tar.gz rust-0e8cc5231154fa0fd54cd173e2dd05a7cce77dc6.zip | |
Properly implement Show for EnumSet
Diffstat (limited to 'src/libcollections/enum_set.rs')
| -rw-r--r-- | src/libcollections/enum_set.rs | 28 |
1 files changed, 27 insertions, 1 deletions
diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 12456e9f79d..02396dc13d1 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -14,8 +14,9 @@ //! representation to hold C-like enum variants. use core::prelude::*; +use core::fmt; -#[deriving(Clone, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, Hash)] /// A specialized `Set` implementation to use enum types. pub struct EnumSet<E> { // We must maintain the invariant that no bits are set @@ -23,6 +24,21 @@ pub struct EnumSet<E> { bits: uint } +impl<E:CLike+fmt::Show> fmt::Show for EnumSet<E> { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + try!(write!(fmt, "{{")); + let mut first = true; + for e in self.iter() { + if !first { + try!(write!(fmt, ", ")); + } + try!(write!(fmt, "{}", e)); + first = false; + } + write!(fmt, "}}") + } +} + /// An interface for casting C-like enum to uint and back. pub trait CLike { /// Converts a C-like enum to a `uint`. @@ -165,6 +181,16 @@ mod test { assert!(e.is_empty()); } + #[test] + fn test_show() { + let mut e = EnumSet::empty(); + assert_eq!("{}", e.to_string().as_slice()); + e.add(A); + assert_eq!("{A}", e.to_string().as_slice()); + e.add(C); + assert_eq!("{A, C}", e.to_string().as_slice()); + } + /////////////////////////////////////////////////////////////////////////// // intersect |
