enumflags2 icon indicating copy to clipboard operation
enumflags2 copied to clipboard

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

Open lolbinarycat opened this issue 1 year ago • 1 comments

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.

lolbinarycat avatar Jun 07 '24 18:06 lolbinarycat

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?

meithecatte avatar Jun 07 '24 19:06 meithecatte