]> git.lizzy.rs Git - enumset.git/commitdiff
Move EnumSetIter into a named struct, in case I want to go fancier in the future.
authorAlissa Rao <lymia@lymiahugs.com>
Tue, 31 Aug 2021 10:33:51 +0000 (03:33 -0700)
committerAlissa Rao <lymia@lymiahugs.com>
Tue, 31 Aug 2021 10:49:46 +0000 (03:49 -0700)
enumset/src/lib.rs

index 98c7977688f6c1b4af9d06b68b0eee7b04503c5a..0de89acaa4255f126e8f04a4f6d43ee55d9df6bc 100644 (file)
@@ -354,7 +354,7 @@ impl <T: EnumSetType> EnumSet<T> {
     /// 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)
+        EnumSetIter::new(*self)
     }
 }
 
@@ -595,21 +595,28 @@ impl <'de, T: EnumSetType> Deserialize<'de> for EnumSet<T> {
 
 /// The iterator used by [`EnumSet`]s.
 #[derive(Clone, Debug)]
-pub struct EnumSetIter<T: EnumSetType>(EnumSet<T>);
+pub struct EnumSetIter<T: EnumSetType> {
+    set: EnumSet<T>,
+}
+impl <T: EnumSetType> EnumSetIter<T> {
+    fn new(set: EnumSet<T>) -> EnumSetIter<T> {
+        EnumSetIter { set }
+    }
+}
 impl <T: EnumSetType> Iterator for EnumSetIter<T> {
     type Item = T;
 
     fn next(&mut self) -> Option<Self::Item> {
-        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<usize>) {
-        let left = self.0.len();
+        let left = self.set.len();
         (left, Some(left))
     }
 }