Skip to content

Conversation

@ssrlive
Copy link
Contributor

@ssrlive ssrlive commented Sep 24, 2025

Add method to iterate over all named flags.

This test suit passed successfully

#[test]
fn test_all_named_flags() {
    // use bitflags::bitflags;

    bitflags! {
        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
        struct Flags: u32 {
            const A = 0b00000001;
            const Zero = 0;
            const B = 0b00000010;
            const C = 0b00000100;
            const CC = 0b00000100;
            const D = 0b10000100;
            const ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
            const AB = Self::A.bits() | Self::B.bits();
            const AC = Self::A.bits() | Self::C.bits();
            const CB = Self::B.bits() | Self::C.bits();
        }
    }

    for flag in Flags::all_named_flags() {
        println!("{:?}", flag);
    }
}

The output is


running 1 test
test test_all_named_flags ... ok

successes:

---- test_all_named_flags stdout ----
Flags(A)
Flags(0x0)
Flags(B)
Flags(C)
Flags(C)
Flags(C | D)
Flags(A | B | C)
Flags(A | B)
Flags(A | C)
Flags(B | C)


successes:
    test_all_named_flags

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 48 filtered out; finished in 0.00s

Add method to iterate over all named flags.
@ssrlive
Copy link
Contributor Author

ssrlive commented Sep 24, 2025

This PR will implement the issue #464

src/traits.rs Outdated
}

/// This method will return an iterator over all named flags (including combinations).
fn all_named_flags() -> impl Iterator<Item = Self> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call this iter_named and move it down to under the iter function on line 217.

We'll also need to write a concrete implementation of the iterator like the other methods do so we don't need to bump our MSRV.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finished

Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this @ssrlive! We'll just need to make a few changes before we can merge this. We should also add a test to the suite here: https://github.com/bitflags/bitflags/tree/main/src/tests

@ssrlive
Copy link
Contributor Author

ssrlive commented Oct 3, 2025

Finished all the tasks.

and add a test case test_iter_named
@ssrlive
Copy link
Contributor Author

ssrlive commented Oct 8, 2025

I have fixed the UI test suits error. Please review it. @KodrAus

Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working through this @ssrlive! This looks good to me.

"Should have 5 composite flags (D, ABC, AB, AC, CB)"
);

println!("All iter_named() tests passed!");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove these additional cases since they're just exercising iterator infrastructure from the standard library and aren't actually asserting on anything, but I'll do that in a follow-up.

@KodrAus KodrAus merged commit 50fab91 into bitflags:main Oct 19, 2025
10 checks passed
@ssrlive
Copy link
Contributor Author

ssrlive commented Oct 19, 2025

Please publish a new version sooner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants