Skip to content

Commit 460bccc

Browse files
authored
Implement Iterator::size_hint and FusedIterator for each of the iterators (#90)
1 parent 71970f7 commit 460bccc

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::fmt::{Binary, Display, Error, Formatter};
3434

3535
pub use range::IndexRange;
3636
use std::cmp::{Ord, Ordering};
37-
use std::iter::{Chain, FromIterator, FusedIterator};
37+
use std::iter::{Chain, ExactSizeIterator, FromIterator, FusedIterator};
3838
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index};
3939

4040
const BITS: usize = std::mem::size_of::<Block>() * 8;
@@ -580,8 +580,16 @@ impl<'a> Iterator for Difference<'a> {
580580
}
581581
None
582582
}
583+
584+
#[inline]
585+
fn size_hint(&self) -> (usize, Option<usize>) {
586+
self.iter.size_hint()
587+
}
583588
}
584589

590+
// Difference will continue to return None once it first returns None.
591+
impl<'a> FusedIterator for Difference<'a> {}
592+
585593
/// An iterator producing elements in the symmetric difference of two sets.
586594
///
587595
/// This struct is created by the [`FixedBitSet::symmetric_difference`] method.
@@ -596,8 +604,16 @@ impl<'a> Iterator for SymmetricDifference<'a> {
596604
fn next(&mut self) -> Option<Self::Item> {
597605
self.iter.next()
598606
}
607+
608+
#[inline]
609+
fn size_hint(&self) -> (usize, Option<usize>) {
610+
self.iter.size_hint()
611+
}
599612
}
600613

614+
// SymmetricDifference will continue to return None once it first returns None.
615+
impl<'a> FusedIterator for SymmetricDifference<'a> {}
616+
601617
/// An iterator producing elements in the intersection of two sets.
602618
///
603619
/// This struct is created by the [`FixedBitSet::intersection`] method.
@@ -619,8 +635,16 @@ impl<'a> Iterator for Intersection<'a> {
619635
}
620636
None
621637
}
638+
639+
#[inline]
640+
fn size_hint(&self) -> (usize, Option<usize>) {
641+
self.iter.size_hint()
642+
}
622643
}
623644

645+
// Intersection will continue to return None once it first returns None.
646+
impl<'a> FusedIterator for Intersection<'a> {}
647+
624648
/// An iterator producing elements in the union of two sets.
625649
///
626650
/// This struct is created by the [`FixedBitSet::union`] method.
@@ -635,8 +659,16 @@ impl<'a> Iterator for Union<'a> {
635659
fn next(&mut self) -> Option<Self::Item> {
636660
self.iter.next()
637661
}
662+
663+
#[inline]
664+
fn size_hint(&self) -> (usize, Option<usize>) {
665+
self.iter.size_hint()
666+
}
638667
}
639668

669+
// Union will continue to return None once it first returns None.
670+
impl<'a> FusedIterator for Union<'a> {}
671+
640672
struct Masks {
641673
first_block: usize,
642674
first_mask: Block,
@@ -694,8 +726,20 @@ impl Iterator for Masks {
694726
Ordering::Greater => None,
695727
}
696728
}
729+
730+
#[inline]
731+
fn size_hint(&self) -> (usize, Option<usize>) {
732+
(self.first_block..=self.last_block).size_hint()
733+
}
697734
}
698735

736+
// Masks will continue to return None once it first returns None.
737+
impl FusedIterator for Masks {}
738+
739+
// Masks's size_hint implementation is exact. It never returns an
740+
// unbounded value and always returns an exact number of values.
741+
impl ExactSizeIterator for Masks {}
742+
699743
/// An iterator producing the indices of the set bit in a set.
700744
///
701745
/// This struct is created by the [`FixedBitSet::ones`] method.
@@ -719,8 +763,16 @@ impl<'a> Iterator for Ones<'a> {
719763
self.bitset ^= t;
720764
Some(self.block_idx + r)
721765
}
766+
767+
#[inline]
768+
fn size_hint(&self) -> (usize, Option<usize>) {
769+
(0, Some(self.remaining_blocks.as_slice().len() * BITS))
770+
}
722771
}
723772

773+
// Ones will continue to return None once it first returns None.
774+
impl<'a> FusedIterator for Ones<'a> {}
775+
724776
/// An iterator producing the indices of the set bit in a set.
725777
///
726778
/// This struct is created by the [`FixedBitSet::ones`] method.

0 commit comments

Comments
 (0)