Non-primitive representation #451
Labels
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-bitflags/bitflags#451
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Though I can count number of times when this was needed on fingers of a single hand, it would be useful if bitflags could generate array backed bitflags.
Instead of storing up to 128 states directly in
u128, one could store however many states they need in[u64; {N / 64}]type.When explicitly defining bitflags this is uncommon, but it's possible when flags are dynamically generated from some data at compile time.
Implementation of this would be different because it would require a loop over
u64array items.Hi @Caellian 👋
This is something you technically can do outside of the
bitflagsmacro by defining your own newtype over[T; N]and implementing theBitstrait for it.bitflagsitself doesn't define a big-int type like what you're describing that you can use, but it is possible to build what you're looking for. We've got an example based on[bool; N]here.The blocker from using a custom bits type in the
bitflagsmacro itself is the need forconst-time bitwise operators, which are only stable on primitive integer types.Until const traits lands, I don't see why not just generate code that performs bitwise ops on array values directly. Crate would probably require a whole separate codegen logic for arrays, but it's nicer than having to implement
Bitsmanually for large flags. Maybe I'm missing something.BTW, this isn't a high priority issue for me, it's just a feature suggestion - I don't even think I'd use it
that oftenever. I mean, if you need >128 booleans on a type, bit twiddling is probably least of your worries. But it would be a nice feature completeness thing.One tweak to my initial suggestion would be to actually make it explicit somehow that the flags are an array (e.g.
FlagsName: [u64; N]) because people will default to not specifying them and expect values being parsed fromTokenStreamto find optimal backing type.This is essentially the problem. The macros here are already complex, and since
macro_rulesmacros don't have good tools for abstraction you end up needing to duplicate a lot of logic. Each knob we add to twiddle in code generation here not only blows out the complexity of the macros, it also blows out the potential for feature mismatch.macro_rulesmacros are good at expanding code, they're not good at making decisions about what code to expand.