Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/vendor/bitflags/src/iter.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / vendor / bitflags / src / iter.rs` 214 lines ยท 5727 bytes Raw Blame History 1 /*! 2 Yield the bits of a source flags value in a set of contained flags values. 3 */ 4 5 use crate::{Flag, Flags}; 6 7 /** 8 An iterator over flags values. 9 10 This iterator will yield flags values for contained, defined flags first, with any remaining bits yielded 11 as a final flags value. 12 */ 13 pub struct Iter { 14 inner: IterNames, 15 done: bool, 16 } 17 18 impl Iter { 19 pub(crate) fn new(flags: &B) -> Self { 20 Iter { 21 inner: IterNames::new(flags), 22 done: false, 23 } 24 } 25 } 26 27 impl Iter { 28 // Used by the `bitflags` macro 29 #[doc(hidden)] 30 pub const fn __private_const_new(flags: &'static [Flag], source: B, remaining: B) -> Self { 31 Iter { 32 inner: IterNames::__private_const_new(flags, source, remaining), 33 done: false, 34 } 35 } 36 } 37 38 impl Iterator for Iter { 39 type Item = B; 40 41 fn next(&mut self) -> Option { 42 match self.inner.next() { 43 Some((_, flag)) => Some(flag), 44 None if !self.done => { 45 self.done = true; 46 47 // After iterating through valid names, if there are any bits left over 48 // then return one final value that includes them. This makes `into_iter` 49 // and `from_iter` roundtrip 50 if !self.inner.remaining().is_empty() { 51 Some(B::from_bits_retain(self.inner.remaining.bits())) 52 } else { 53 None 54 } 55 } 56 None => None, 57 } 58 } 59 } 60 61 /** 62 An iterator over flags values. 63 64 This iterator only yields flags values for contained, defined, named flags. Any remaining bits 65 won't be yielded, but can be found with the [`IterNames::remaining`] method. 66 */ 67 pub struct IterNames { 68 flags: &'static [Flag], 69 idx: usize, 70 source: B, 71 remaining: B, 72 } 73 74 impl IterNames { 75 pub(crate) fn new(flags: &B) -> Self { 76 IterNames { 77 flags: B::FLAGS, 78 idx: 0, 79 remaining: B::from_bits_retain(flags.bits()), 80 source: B::from_bits_retain(flags.bits()), 81 } 82 } 83 } 84 85 impl IterNames { 86 // Used by the bitflags macro 87 #[doc(hidden)] 88 pub const fn __private_const_new(flags: &'static [Flag], source: B, remaining: B) -> Self { 89 IterNames { 90 flags, 91 idx: 0, 92 remaining, 93 source, 94 } 95 } 96 97 /// Get a flags value of any remaining bits that haven't been yielded yet. 98 /// 99 /// Once the iterator has finished, this method can be used to 100 /// check whether or not there are any bits that didn't correspond 101 /// to a contained, defined, named flag remaining. 102 pub fn remaining(&self) -> &B { 103 &self.remaining 104 } 105 } 106 107 impl Iterator for IterNames { 108 type Item = (&'static str, B); 109 110 fn next(&mut self) -> Option { 111 while let Some(flag) = self.flags.get(self.idx) { 112 // Short-circuit if our state is empty 113 if self.remaining.is_empty() { 114 return None; 115 } 116 117 self.idx += 1; 118 119 // Skip unnamed flags 120 if flag.name().is_empty() { 121 continue; 122 } 123 124 let bits = flag.value().bits(); 125 126 // If the flag is set in the original source _and_ it has bits that haven't 127 // been covered by a previous flag yet then yield it. These conditions cover 128 // two cases for multi-bit flags: 129 // 130 // 1. When flags partially overlap, such as ``0b00000001`` and ``0b00000101``, we'll 131 // yield both flags. 132 // 2. When flags fully overlap, such as in convenience flags that are a shorthand for others, 133 // we won't yield both flags. 134 if self.source.contains(B::from_bits_retain(bits)) 135 && self.remaining.intersects(B::from_bits_retain(bits)) 136 { 137 self.remaining.remove(B::from_bits_retain(bits)); 138 139 return Some((flag.name(), B::from_bits_retain(bits))); 140 } 141 } 142 143 None 144 } 145 } 146 147 /** 148 An iterator over all defined named flags. 149 150 This iterator will yield flags values for all defined named flags, regardless of 151 whether they are contained in a particular flags value. 152 */ 153 pub struct IterDefinedNames { 154 flags: &'static [Flag], 155 idx: usize, 156 } 157 158 impl IterDefinedNames { 159 pub(crate) fn new() -> Self { 160 IterDefinedNames { 161 flags: B::FLAGS, 162 idx: 0, 163 } 164 } 165 } 166 167 impl Iterator for IterDefinedNames { 168 type Item = (&'static str, B); 169 170 fn next(&mut self) -> Option { 171 while let Some(flag) = self.flags.get(self.idx) { 172 self.idx += 1; 173 174 // Only yield named flags 175 if flag.is_named() { 176 return Some((flag.name(), B::from_bits_retain(flag.value().bits()))); 177 } 178 } 179 180 None 181 } 182 } 183 184 /** 185 An iterator over all defined names for a specific flags value. 186 */ 187 pub struct IterEqualNames { 188 inner: IterDefinedNames, 189 bits: T::Bits, 190 } 191 192 impl IterEqualNames { 193 #[inline] 194 pub(crate) fn new(value: &T) -> Self { 195 Self { 196 inner: ::iter_defined_names(), 197 bits: value.bits(), 198 } 199 } 200 } 201 202 impl Iterator for IterEqualNames { 203 type Item = &'static str; 204 205 #[inline] 206 fn next(&mut self) -> Option { 207 for (n, f) in self.inner.by_ref() { 208 if f.bits() == self.bits { 209 return Some(n); 210 } 211 } 212 None 213 } 214 }