Coverage Report

Created: 2025-12-31 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/enumflags2-0.7.12/src/iter.rs
Line
Count
Source
1
use crate::{BitFlag, BitFlags, BitFlagNum};
2
use core::iter::{FromIterator, FusedIterator};
3
4
impl<T> BitFlags<T>
5
where
6
    T: BitFlag,
7
{
8
    /// Iterate over the `BitFlags`.
9
    ///
10
    /// ```
11
    /// # use enumflags2::{bitflags, make_bitflags};
12
    /// # #[bitflags]
13
    /// # #[derive(Clone, Copy, PartialEq, Debug)]
14
    /// # #[repr(u8)]
15
    /// # enum MyFlag {
16
    /// #     A = 1 << 0,
17
    /// #     B = 1 << 1,
18
    /// #     C = 1 << 2,
19
    /// # }
20
    /// let flags = make_bitflags!(MyFlag::{A | C});
21
    ///
22
    /// flags.iter()
23
    ///     .for_each(|flag| println!("{:?}", flag));
24
    /// ```
25
    #[inline]
26
0
    pub fn iter(self) -> Iter<T> {
27
0
        Iter { rest: self }
28
0
    }
Unexecuted instantiation: <enumflags2::BitFlags<landlock::fs::AccessFs, u64>>::iter
Unexecuted instantiation: <enumflags2::BitFlags<landlock::net::AccessNet, u64>>::iter
Unexecuted instantiation: <enumflags2::BitFlags<landlock::scope::Scope, u64>>::iter
Unexecuted instantiation: <enumflags2::BitFlags<_>>::iter
29
}
30
31
impl<T: BitFlag> IntoIterator for BitFlags<T> {
32
    type IntoIter = Iter<T>;
33
    type Item = T;
34
35
0
    fn into_iter(self) -> Self::IntoIter {
36
0
        self.iter()
37
0
    }
38
}
39
40
/// Iterator that yields each flag set in a `BitFlags`.
41
#[derive(Clone, Debug)]
42
pub struct Iter<T: BitFlag> {
43
    rest: BitFlags<T>,
44
}
45
46
impl<T> Iterator for Iter<T>
47
where
48
    T: BitFlag,
49
{
50
    type Item = T;
51
52
0
    fn next(&mut self) -> Option<Self::Item> {
53
0
        if self.rest.is_empty() {
54
0
            None
55
        } else {
56
            // SAFETY: `flag` will be a single bit, because
57
            // x & -x = x & (~x + 1), and the increment causes only one 0 -> 1 transition.
58
            // The invariant of `from_bits_unchecked` is satisfied, because bits & x
59
            // is a subset of bits, which we know are the valid bits.
60
            unsafe {
61
0
                let bits = self.rest.bits();
62
0
                let flag: T::Numeric = bits & bits.wrapping_neg();
63
0
                let flag: T = core::mem::transmute_copy(&flag);
64
0
                self.rest = BitFlags::from_bits_unchecked(bits & (bits - BitFlagNum::ONE));
65
0
                Some(flag)
66
            }
67
        }
68
0
    }
Unexecuted instantiation: <enumflags2::iter::Iter<landlock::fs::AccessFs> as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <enumflags2::iter::Iter<landlock::net::AccessNet> as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <enumflags2::iter::Iter<landlock::scope::Scope> as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <enumflags2::iter::Iter<_> as core::iter::traits::iterator::Iterator>::next
69
70
0
    fn size_hint(&self) -> (usize, Option<usize>) {
71
0
        let l = self.rest.len();
72
0
        (l, Some(l))
73
0
    }
74
}
75
76
impl<T> ExactSizeIterator for Iter<T>
77
where
78
    T: BitFlag,
79
{
80
0
    fn len(&self) -> usize {
81
0
        self.rest.len()
82
0
    }
83
}
84
85
impl<T: BitFlag> FusedIterator for Iter<T> {}
86
87
impl<T, B> FromIterator<B> for BitFlags<T>
88
where
89
    T: BitFlag,
90
    B: Into<BitFlags<T>>,
91
{
92
    #[inline]
93
0
    fn from_iter<I>(it: I) -> BitFlags<T>
94
0
    where
95
0
        I: IntoIterator<Item = B>,
96
    {
97
0
        it.into_iter()
98
0
            .fold(BitFlags::empty(), |acc, flag| acc | flag)
99
0
    }
100
}
101
102
impl<T, B> Extend<B> for BitFlags<T>
103
where
104
    T: BitFlag,
105
    B: Into<BitFlags<T>>,
106
{
107
    #[inline]
108
0
    fn extend<I>(&mut self, it: I)
109
0
    where
110
0
        I: IntoIterator<Item = B>,
111
    {
112
0
        *self = it.into_iter().fold(*self, |acc, flag| acc | flag)
113
0
    }
114
}