/// Note that iterator invalidation is impossible as the iterator contains a copy of this type,
/// rather than holding a reference to it.
pub fn iter(&self) -> EnumSetIter<T> {
- EnumSetIter(*self, 0)
+ EnumSetIter(*self)
}
}
/// The iterator used by [`EnumSet`]s.
#[derive(Clone, Debug)]
-pub struct EnumSetIter<T: EnumSetType>(EnumSet<T>, u32);
+pub struct EnumSetIter<T: EnumSetType>(EnumSet<T>);
impl <T: EnumSetType> Iterator for EnumSetIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
- while self.1 < EnumSet::<T>::bit_width() {
- let bit = self.1;
- self.1 += 1;
- if self.0.__priv_repr.has_bit(bit) {
- return unsafe { Some(T::enum_from_u32(bit)) }
- }
+ if self.0.is_empty() {
+ None
+ } else {
+ let bit = self.0.__priv_repr.trailing_zeros();
+ self.0.__priv_repr.remove_bit(bit);
+ unsafe { Some(T::enum_from_u32(bit)) }
}
- None
}
fn size_hint(&self) -> (usize, Option<usize>) {
- let left = self.0.__priv_repr.count_remaining_ones(self.1);
+ let left = self.0.len();
(left, Some(left))
}
}
fn count_ones(&self) -> u32;
fn count_remaining_ones(&self, cursor: u32) -> usize;
fn leading_zeros(&self) -> u32;
+ fn trailing_zeros(&self) -> u32;
fn and_not(&self, other: Self) -> Self;
fn count_ones(&self) -> u32 { (*self).count_ones() }
fn leading_zeros(&self) -> u32 { (*self).leading_zeros() }
+ fn trailing_zeros(&self) -> u32 { (*self).trailing_zeros() }
fn and_not(&self, other: Self) -> Self { (*self) & !other }