1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use crate::{BitFlag, BitFlags, BitFlagNum};
use core::iter::{FromIterator, FusedIterator};

impl<T> BitFlags<T>
where
    T: BitFlag,
{
    /// Iterate over the `BitFlags`.
    ///
    /// ```
    /// # use enumflags2::{bitflags, make_bitflags};
    /// # #[bitflags]
    /// # #[derive(Clone, Copy, PartialEq, Debug)]
    /// # #[repr(u8)]
    /// # enum MyFlag {
    /// #     A = 1 << 0,
    /// #     B = 1 << 1,
    /// #     C = 1 << 2,
    /// # }
    /// let flags = make_bitflags!(MyFlag::{A | C});
    ///
    /// flags.iter()
    ///     .for_each(|flag| println!("{:?}", flag));
    /// ```
    #[inline]
    pub fn iter(self) -> Iter<T> {
        Iter { rest: self }
    }
}

impl<T: BitFlag> IntoIterator for BitFlags<T> {
    type IntoIter = Iter<T>;
    type Item = T;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// Iterator that yields each flag set in a `BitFlags`.
#[derive(Clone, Debug)]
pub struct Iter<T: BitFlag> {
    rest: BitFlags<T>,
}

impl<T> Iterator for Iter<T>
where
    T: BitFlag,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.rest.is_empty() {
            None
        } else {
            // SAFETY: `flag` will be a single bit, because
            // x & -x = x & (~x + 1), and the increment causes only one 0 -> 1 transition.
            // The invariant of `from_bits_unchecked` is satisfied, because bits & x
            // is a subset of bits, which we know are the valid bits.
            unsafe {
                let bits = self.rest.bits();
                let flag: T::Numeric = bits & bits.wrapping_neg();
                let flag: T = core::mem::transmute_copy(&flag);
                self.rest = BitFlags::from_bits_unchecked(bits & (bits - BitFlagNum::ONE));
                Some(flag)
            }
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let l = self.rest.len();
        (l, Some(l))
    }
}

impl<T> ExactSizeIterator for Iter<T>
where
    T: BitFlag,
{
    fn len(&self) -> usize {
        self.rest.len()
    }
}

impl<T: BitFlag> FusedIterator for Iter<T> {}

impl<T, B> FromIterator<B> for BitFlags<T>
where
    T: BitFlag,
    B: Into<BitFlags<T>>,
{
    #[inline]
    fn from_iter<I>(it: I) -> BitFlags<T>
    where
        I: IntoIterator<Item = B>,
    {
        it.into_iter()
            .fold(BitFlags::empty(), |acc, flag| acc | flag)
    }
}

impl<T, B> Extend<B> for BitFlags<T>
where
    T: BitFlag,
    B: Into<BitFlags<T>>,
{
    #[inline]
    fn extend<I>(&mut self, it: I)
    where
        I: IntoIterator<Item = B>,
    {
        *self = it.into_iter().fold(*self, |acc, flag| acc | flag)
    }
}