@@ -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+ }
0 commit comments