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

allow BitFlags<T> to have a different representation from T #59

Open
lolbinarycat opened this issue Jun 7, 2024 · 1 comment
Open

Comments

@lolbinarycat
Copy link

I was looking for a way to compactly represent a set of enums in a project, and for a second i thought this crate was exactly what i needed, but then i realized that the enum T must have a bitflag-like representation. this is a problem, since i'm already using my enum as an array index, so all its values need to be contiguous.

extracting the index of a bit from a set of bitflags is nontrivial, however making a bitflag from an index is just a single left shift.

i propose a new form, #[bitflags(index)], that gives the enums values linearly (same as the default rust behavior for unit enums).

in order to not confuse other crates that assume impl BitFlag for T means they can cast an enum to an integer in order to get the bitflag value, this would derive a different trait, BitFlagIndex, for the enum. BitFlagIndex would function mostly the same as BitFlag, but with an implicit shift when converting to BitFlags<T> where T: BitFlagIndex.

this would take a moderate effort, but i'm willing to write up a PR if there's a decent chance it'll get merged.

@meithecatte
Copy link
Owner

Hm, this is definitely an important usecase. I'd support implementing this, provided that this isn't already solved by another crate.

I took a brief stroll through lib.rs and found the enumset crate which sounds like what you need. Have you looked at that?

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

2 participants