Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to iterate through flags #28

Closed
gsingh93 opened this issue Jan 2, 2016 · 7 comments
Closed

Add ability to iterate through flags #28

gsingh93 opened this issue Jan 2, 2016 · 7 comments

Comments

@gsingh93
Copy link

gsingh93 commented Jan 2, 2016

Iterating over each each possible flag value would be a cool feature. I'm currently making an array with each constant value and then iterating over that.

@RReverser
Copy link

@alexcrichton Is this something that would be accepted in the library?

@alexcrichton
Copy link
Contributor

Seems plausible!

@djg
Copy link

djg commented Mar 29, 2018

I was just thinking about implementing this feature for some code I'm working on, if it's not already implemented.

@Dushistov
Copy link

Dushistov commented Apr 23, 2019

May be generate const array or const function that return array that contains
all values?

Then implementation of iteration will be strait forward,
plus the array may also contains names:

const fn all_values_names() -> [(Flag, &'static str); N]

This helps in implementation of custom serialization/deserialization (#147) or custom debug (#168).

What do you think?

Example of possible use-case: I want parse / generate strings like: "ab bc cd" <-> Flags::AB | Flags::BC | Flags::CD,

to generate string I need iteration plus names of constants in lower-case,
to deserialize I need names in lower-case

@sam0x17

This comment has been minimized.

@KodrAus
Copy link
Member

KodrAus commented May 4, 2022

In #278 we merged an implementation that allows iterating over flags and their names. It effectively looks like:

pub fn iter(self) -> impl Iterator<Item = (&'static str, Self)>

It effectively does what @Dushistov described, but internally. There's still design work to be done on this though, IntoIterator to implement, and some refactoring of how we generate flags types so we can make that iter function return a concrete type instead of impl Trait so I'll keep this ticket open for now to consider those.

@KodrAus
Copy link
Member

KodrAus commented Oct 8, 2022

This is now implemented in 2.0.0-rc.1

@KodrAus KodrAus closed this as completed Oct 8, 2022
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

No branches or pull requests

7 participants