From: Alissa Rao Date: Tue, 31 Aug 2021 10:33:51 +0000 (-0700) Subject: Move EnumSetIter into a named struct, in case I want to go fancier in the future. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=689c91c511de13e0eadc1c69099bb0218f65f79c;p=enumset.git Move EnumSetIter into a named struct, in case I want to go fancier in the future. --- diff --git a/enumset/src/lib.rs b/enumset/src/lib.rs index 98c7977..0de89ac 100644 --- a/enumset/src/lib.rs +++ b/enumset/src/lib.rs @@ -354,7 +354,7 @@ impl EnumSet { /// 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 { - EnumSetIter(*self) + EnumSetIter::new(*self) } } @@ -595,21 +595,28 @@ impl <'de, T: EnumSetType> Deserialize<'de> for EnumSet { /// The iterator used by [`EnumSet`]s. #[derive(Clone, Debug)] -pub struct EnumSetIter(EnumSet); +pub struct EnumSetIter { + set: EnumSet, +} +impl EnumSetIter { + fn new(set: EnumSet) -> EnumSetIter { + EnumSetIter { set } + } +} impl Iterator for EnumSetIter { type Item = T; fn next(&mut self) -> Option { - if self.0.is_empty() { + if self.set.is_empty() { None } else { - let bit = self.0.__priv_repr.trailing_zeros(); - self.0.__priv_repr.remove_bit(bit); + let bit = self.set.__priv_repr.trailing_zeros(); + self.set.__priv_repr.remove_bit(bit); unsafe { Some(T::enum_from_u32(bit)) } } } fn size_hint(&self) -> (usize, Option) { - let left = self.0.len(); + let left = self.set.len(); (left, Some(left)) } }