Skip to content

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

@lolbinarycat

Description

@lolbinarycat

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions