about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-11-08 16:41:39 +0000
committerbors <bors@rust-lang.org>2014-11-08 16:41:39 +0000
commitb80edf1d1257bf521fd9e62b732b6302258374e7 (patch)
treea6c5eded582e12b8a6a624bdc7947a6743217684
parentfa2983a1b7fc77dad37d49dcebeda3c25aac1e3f (diff)
parenta79d4be39cf99ed4dba1c757400a3ea708956653 (diff)
downloadrust-b80edf1d1257bf521fd9e62b732b6302258374e7.tar.gz
rust-b80edf1d1257bf521fd9e62b732b6302258374e7.zip
auto merge of #18740 : jbcrail/rust/implement-enum-set-len, r=alexcrichton
This commit adds the missing EnumSet method mentioned by @Gankro.

cc #18424
-rw-r--r--src/libcollections/enum_set.rs21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs
index 1acdaef9c91..68991628b22 100644
--- a/src/libcollections/enum_set.rs
+++ b/src/libcollections/enum_set.rs
@@ -18,7 +18,6 @@ use core::fmt;
 
 // FIXME(conventions): implement BitXor
 // FIXME(contentions): implement union family of methods? (general design may be wrong here)
-// FIXME(conventions): implement len
 
 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
 /// A specialized `Set` implementation to use enum types.
@@ -92,6 +91,12 @@ impl<E:CLike> EnumSet<E> {
         EnumSet {bits: 0}
     }
 
+    /// Returns the number of elements in the given `EnumSet`.
+    #[unstable = "matches collection reform specification, waiting for dust to settle"]
+    pub fn len(&self) -> uint {
+        self.bits.count_ones()
+    }
+
     /// Returns true if the `EnumSet` is empty.
     #[unstable = "matches collection reform specification, waiting for dust to settle"]
     pub fn is_empty(&self) -> bool {
@@ -269,6 +274,20 @@ mod test {
         assert_eq!("{A, C}", e.to_string().as_slice());
     }
 
+    #[test]
+    fn test_len() {
+        let mut e = EnumSet::new();
+        assert_eq!(e.len(), 0);
+        e.insert(A);
+        e.insert(B);
+        e.insert(C);
+        assert_eq!(e.len(), 3);
+        e.remove(&A);
+        assert_eq!(e.len(), 2);
+        e.clear();
+        assert_eq!(e.len(), 0);
+    }
+
     ///////////////////////////////////////////////////////////////////////////
     // intersect