From a0438143dec68fea83e3e4d28c120f7d14a9572d Mon Sep 17 00:00:00 2001 From: nham Date: Fri, 1 Aug 2014 16:48:06 -0400 Subject: [PATCH] collections: Implement FromIterator/Extendable for BitvSet --- src/libcollections/bitv.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 3e1160b45ee..02714c728ec 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -978,6 +978,21 @@ impl Default for BitvSet { fn default() -> BitvSet { BitvSet::new() } } +impl FromIterator for BitvSet { + fn from_iter>(iterator: I) -> BitvSet { + let mut ret = BitvSet::new(); + ret.extend(iterator); + ret + } +} + +impl Extendable for BitvSet { + #[inline] + fn extend>(&mut self, iterator: I) { + self.get_mut_ref().extend(iterator); + } +} + impl BitvSet { /// Create a new bit vector set with initially no contents. /// @@ -1958,6 +1973,17 @@ mod tests { assert_eq!(bitv.to_string().as_slice(), "1011"); } + #[test] + fn test_bitv_set_from_bools() { + let bools = vec![true, false, true, true]; + let a: BitvSet = bools.iter().map(|n| *n).collect(); + let mut b = BitvSet::new(); + b.insert(0); + b.insert(2); + b.insert(3); + assert_eq!(a, b); + } + #[test] fn test_to_bools() { let bools = vec!(false, false, true, false, false, true, true, false); @@ -1977,7 +2003,7 @@ mod tests { #[test] fn test_bitv_set_iterator() { let bools = [true, false, true, true]; - let bitv = BitvSet::from_bitv(bools.iter().map(|n| *n).collect()); + let bitv: BitvSet = bools.iter().map(|n| *n).collect(); let idxs: Vec = bitv.iter().collect(); assert_eq!(idxs, vec!(0, 2, 3));