Implement iterator for all named flags by ssrlive · Pull Request #465 · bitflags/bitflags · GitHub
Skip to content

Implement iterator for all named flags - #465

Merged
KodrAus merged 3 commits into
bitflags:mainfrom
ssrlive:main
Oct 19, 2025
Merged

KodrAus merged 3 commits into
bitflags:mainfrom
ssrlive:main

Conversation

@ssrlive

@ssrlive ssrlive commented Sep 24, 2025 •

Copy link
Copy Markdown
Contributor

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

ssrlive commented Sep 24, 2025 •

Copy link
Copy Markdown
Contributor Author

Comment thread 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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

finished

@KodrAus KodrAus left a comment

Copy link
Copy Markdown
Member

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

ssrlive commented Oct 3, 2025

Copy link
Copy Markdown
Contributor Author

Finished all the tasks.

@ssrlive

ssrlive commented Oct 8, 2025

Copy link
Copy Markdown
Contributor Author

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

@KodrAus KodrAus left a comment

Copy link
Copy Markdown
Member

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.

Comment thread src/tests/iter_named.rs
"Should have 5 composite flags (D, ABC, AB, AC, CB)"
);

println!("All iter_named() tests passed!");

Copy link
Copy Markdown
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

ssrlive commented Oct 19, 2025

Copy link
Copy Markdown
Contributor Author

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