Skip to content

Commit 921cfd8

Browse files
authored
Merge pull request #84 from ltratt/fusediterator
Implement `FusedIterator` for `Iter{Set,Unset}Bits`
2 parents cbece1f + 890fb13 commit 921cfd8

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/lib.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
cmp::{min, PartialEq},
88
fmt::{self, Debug},
99
hash::{Hash, Hasher},
10-
iter::{DoubleEndedIterator, FromIterator},
10+
iter::{DoubleEndedIterator, FromIterator, FusedIterator},
1111
mem::{replace, size_of},
1212
ops::{
1313
Bound::{Excluded, Included, Unbounded},
@@ -1182,6 +1182,8 @@ impl<T: Debug + PrimInt> DoubleEndedIterator for IterSetBits<'_, T> {
11821182
}
11831183
}
11841184

1185+
impl<T: Debug + PrimInt> FusedIterator for IterSetBits<'_, T> {}
1186+
11851187
#[derive(Clone)]
11861188
pub struct IterUnsetBits<'a, T: 'a> {
11871189
vob: &'a Vob<T>,
@@ -1260,6 +1262,8 @@ impl<T: Debug + PrimInt> DoubleEndedIterator for IterUnsetBits<'_, T> {
12601262
}
12611263
}
12621264

1265+
impl<T: Debug + PrimInt> FusedIterator for IterUnsetBits<'_, T> {}
1266+
12631267
impl<T: Debug + PrimInt> PartialEq for Vob<T> {
12641268
fn eq(&self, other: &Self) -> bool {
12651269
if self.len != other.len {
@@ -1914,6 +1918,21 @@ mod tests {
19141918
t(&v1, 128.., vec![129, 130, 256]);
19151919
}
19161920

1921+
#[test]
1922+
fn test_fused() {
1923+
let v1 = vob![true];
1924+
let mut i1 = v1.iter_set_bits(..);
1925+
assert!(i1.next().is_some());
1926+
assert!(i1.next().is_none());
1927+
assert!(i1.next().is_none());
1928+
1929+
let v2 = vob![false];
1930+
let mut i2 = v2.iter_unset_bits(..);
1931+
assert!(i2.next().is_some());
1932+
assert!(i2.next().is_none());
1933+
assert!(i2.next().is_none());
1934+
}
1935+
19171936
#[test]
19181937
fn test_eq() {
19191938
let v1 = Vob::from_iter(vec![true, false]);

0 commit comments

Comments
 (0)