Skip to content

Commit b80edf1

Browse files
committedNov 8, 2014
auto merge of #18740 : jbcrail/rust/implement-enum-set-len, r=alexcrichton
This commit adds the missing EnumSet method mentioned by @gankro. cc #18424
2 parents fa2983a + a79d4be commit b80edf1

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed
 

‎src/libcollections/enum_set.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use core::fmt;
1818

1919
// FIXME(conventions): implement BitXor
2020
// FIXME(contentions): implement union family of methods? (general design may be wrong here)
21-
// FIXME(conventions): implement len
2221

2322
#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
2423
/// A specialized `Set` implementation to use enum types.
@@ -92,6 +91,12 @@ impl<E:CLike> EnumSet<E> {
9291
EnumSet {bits: 0}
9392
}
9493

94+
/// Returns the number of elements in the given `EnumSet`.
95+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
96+
pub fn len(&self) -> uint {
97+
self.bits.count_ones()
98+
}
99+
95100
/// Returns true if the `EnumSet` is empty.
96101
#[unstable = "matches collection reform specification, waiting for dust to settle"]
97102
pub fn is_empty(&self) -> bool {
@@ -269,6 +274,20 @@ mod test {
269274
assert_eq!("{A, C}", e.to_string().as_slice());
270275
}
271276

277+
#[test]
278+
fn test_len() {
279+
let mut e = EnumSet::new();
280+
assert_eq!(e.len(), 0);
281+
e.insert(A);
282+
e.insert(B);
283+
e.insert(C);
284+
assert_eq!(e.len(), 3);
285+
e.remove(&A);
286+
assert_eq!(e.len(), 2);
287+
e.clear();
288+
assert_eq!(e.len(), 0);
289+
}
290+
272291
///////////////////////////////////////////////////////////////////////////
273292
// intersect
274293

0 commit comments

Comments
 (0)
Please sign in to comment.