diff --git a/src/auto.rs b/src/auto.rs index cf11832..ef685e1 100644 --- a/src/auto.rs +++ b/src/auto.rs @@ -417,6 +417,14 @@ impl FromIterator for BV { } } +impl Extend for BV { + fn extend>(&mut self, iter: T) { + let iter = iter.into_iter(); + self.reserve(iter.size_hint().0); + iter.for_each(|b| self.push(b)); + } +} + // ------------------------------------------------------------------------------------------------ // BV - Formatting traits // ------------------------------------------------------------------------------------------------ diff --git a/src/dynamic.rs b/src/dynamic.rs index 629515f..bfa22dc 100644 --- a/src/dynamic.rs +++ b/src/dynamic.rs @@ -584,6 +584,14 @@ impl FromIterator for BVD { } } +impl Extend for BVD { + fn extend>(&mut self, iter: T) { + let iter = iter.into_iter(); + self.reserve(iter.size_hint().0); + iter.for_each(|b| self.push(b)); + } +} + // ------------------------------------------------------------------------------------------------ // BVD - Formatting traits // ------------------------------------------------------------------------------------------------ diff --git a/src/fixed.rs b/src/fixed.rs index 784f330..2223963 100644 --- a/src/fixed.rs +++ b/src/fixed.rs @@ -577,6 +577,12 @@ where } } +impl Extend for BVF { + fn extend>(&mut self, iter: T) { + iter.into_iter().for_each(|b| self.push(b)); + } +} + // ------------------------------------------------------------------------------------------------ // BVF - Formatting traits // ------------------------------------------------------------------------------------------------ diff --git a/src/tests/iter.rs b/src/tests/iter.rs index b70976f..4fd6972 100644 --- a/src/tests/iter.rs +++ b/src/tests/iter.rs @@ -91,3 +91,31 @@ fn from_iter_bv() { from_iter_inner::(64); from_iter_inner::(256); } + +fn extend_inner>(capacity: usize) { + let mut rng = thread_rng(); + let bits: Vec = (0..capacity) + .map(|_| Bit::from(rng.gen::())) + .collect(); + let mut bv = B::with_capacity(capacity / 2); + bv.extend(bits.iter().copied()); + for (b1, &b2) in bv.iter().zip(bits.iter()) { + assert_eq!(b1, b2); + } +} + +#[test] +fn extend_bvf() { + bvf_inner_unroll_cap!(extend_inner, {u8, u16, u32, u64, u128}, {1, 2, 3, 4, 5}); +} + +#[test] +fn extend_bvd() { + extend_inner::(256); +} + +#[test] +fn extend_bv() { + extend_inner::(64); + extend_inner::(256); +}