Skip to content

Commit 409666e

Browse files
committed
rename iter_named to iter_defined_names
1 parent 50fab91 commit 409666e

File tree

5 files changed

+102
-218
lines changed

5 files changed

+102
-218
lines changed

src/iter.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,35 +145,35 @@ impl<B: Flags> Iterator for IterNames<B> {
145145
}
146146

147147
/**
148-
An iterator over all named flags.
148+
An iterator over all defined named flags.
149149
150150
This iterator will yield flags values for all defined named flags, regardless of
151151
whether they are contained in a particular flags value.
152152
*/
153-
pub struct IterNamed<B: 'static> {
153+
pub struct IterDefinedNames<B: 'static> {
154154
flags: &'static [Flag<B>],
155155
idx: usize,
156156
}
157157

158-
impl<B: Flags> IterNamed<B> {
158+
impl<B: Flags> IterDefinedNames<B> {
159159
pub(crate) fn new() -> Self {
160-
IterNamed {
160+
IterDefinedNames {
161161
flags: B::FLAGS,
162162
idx: 0,
163163
}
164164
}
165165
}
166166

167-
impl<B: Flags> Iterator for IterNamed<B> {
168-
type Item = B;
167+
impl<B: Flags> Iterator for IterDefinedNames<B> {
168+
type Item = (&'static str, B);
169169

170170
fn next(&mut self) -> Option<Self::Item> {
171171
while let Some(flag) = self.flags.get(self.idx) {
172172
self.idx += 1;
173173

174174
// Only yield named flags
175175
if flag.is_named() {
176-
return Some(B::from_bits_retain(flag.value().bits()));
176+
return Some((flag.name(), B::from_bits_retain(flag.value().bits())));
177177
}
178178
}
179179

src/tests.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ mod intersects;
2020
mod is_all;
2121
mod is_empty;
2222
mod iter;
23-
mod iter_named;
2423
mod parser;
2524
mod remove;
2625
mod symmetric_difference;

src/tests/iter.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,93 @@ mod iter_names {
207207
);
208208
}
209209
}
210+
211+
mod iter_defined_names {
212+
use crate::Flags;
213+
214+
#[test]
215+
fn test_defined_names() {
216+
bitflags! {
217+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
218+
struct TestFlags: u32 {
219+
const A = 0b00000001;
220+
const ZERO = 0;
221+
const B = 0b00000010;
222+
const C = 0b00000100;
223+
const CC = Self::C.bits();
224+
const D = 0b10000100;
225+
const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
226+
const AB = Self::A.bits() | Self::B.bits();
227+
const AC = Self::A.bits() | Self::C.bits();
228+
const CB = Self::B.bits() | Self::C.bits();
229+
}
230+
}
231+
232+
// Test all named flags produced by the iterator
233+
let all_named: Vec<(&'static str, TestFlags)> = TestFlags::iter_defined_names().collect();
234+
235+
// Verify all named flags are included
236+
let expected_flags = vec![
237+
("A", TestFlags::A),
238+
("ZERO", TestFlags::ZERO),
239+
("B", TestFlags::B),
240+
("C", TestFlags::C),
241+
// Note: CC and C have the same bit value, but both are named flags
242+
("CC", TestFlags::CC),
243+
("D", TestFlags::D),
244+
("ABC", TestFlags::ABC),
245+
("AB", TestFlags::AB),
246+
("AC", TestFlags::AC),
247+
("CB", TestFlags::CB),
248+
];
249+
250+
assert_eq!(
251+
all_named.len(),
252+
expected_flags.len(),
253+
"Should have 10 named flags"
254+
);
255+
256+
// Verify each expected flag is in the result
257+
for expected_flag in &expected_flags {
258+
assert!(
259+
all_named.contains(expected_flag),
260+
"Missing flag: {:?}",
261+
expected_flag
262+
);
263+
}
264+
265+
// Test if iterator order is consistent with definition order
266+
let flags_in_order: Vec<(&'static str, TestFlags)> =
267+
TestFlags::iter_defined_names().collect();
268+
assert_eq!(
269+
flags_in_order, expected_flags,
270+
"Flag order should match definition order"
271+
);
272+
273+
// Test that iterator can be used multiple times
274+
let first_iteration: Vec<(&'static str, TestFlags)> =
275+
TestFlags::iter_defined_names().collect();
276+
let second_iteration: Vec<(&'static str, TestFlags)> =
277+
TestFlags::iter_defined_names().collect();
278+
assert_eq!(
279+
first_iteration, second_iteration,
280+
"Multiple iterations should produce the same result"
281+
);
282+
283+
// Test consistency with FLAGS constant
284+
let flags_from_iter: std::collections::HashSet<u32> = TestFlags::iter_defined_names()
285+
.map(|(_, f)| f.bits())
286+
.collect();
287+
288+
let flags_from_const: std::collections::HashSet<u32> = TestFlags::FLAGS
289+
.iter()
290+
.filter(|f| f.is_named())
291+
.map(|f| f.value().bits())
292+
.collect();
293+
294+
assert_eq!(
295+
flags_from_iter, flags_from_const,
296+
"iter_defined_names() should be consistent with named flags in FLAGS"
297+
);
298+
}
299+
}

src/tests/iter_named.rs

Lines changed: 0 additions & 205 deletions
This file was deleted.

src/traits.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ pub trait Flags: Sized + 'static {
210210
iter::Iter::new(self)
211211
}
212212

213-
/// This method will return an iterator over all named flags (including combinations).
214-
fn iter_named() -> iter::IterNamed<Self> {
215-
iter::IterNamed::new()
216-
}
217-
218213
/// Yield a set of contained named flags values.
219214
///
220215
/// This method is like [`Flags::iter`], except only yields bits in contained named flags.
@@ -223,6 +218,11 @@ pub trait Flags: Sized + 'static {
223218
iter::IterNames::new(self)
224219
}
225220

221+
/// Yield a set of all named flags defined by [`Self::FLAGS`].
222+
fn iter_defined_names() -> iter::IterDefinedNames<Self> {
223+
iter::IterDefinedNames::new()
224+
}
225+
226226
/// Whether all bits in this flags value are unset.
227227
fn is_empty(&self) -> bool {
228228
self.bits() == Self::Bits::EMPTY

0 commit comments

Comments
 (0)