Support for explicit endianness #455
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#455
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?
When working with hardware or network packets it is sometimes necessary to access bitfields which may have a defined endianness which doesn't match the host's endianness. It would be useful if
bitflagscould be used to define a bitflags type with an explicit endianness.I'm not sure what the best way to achieve this would be. If working with
zerocopy, one approach would be to use e.g.zerocopy::byteorder::little_endian::U32as the representation for a bitflags type.#451 might also be related.
Ah I see the issue here is that you're re-interpreting memory-mapped data as some flags type? If that wasn't the case then I'd expect you'd be able to use
from_bits(u32::from_le_bytes(..))to handle endianness.The only thing stopping us from supporting something like
zerocopy::byteorder::little_endian::U32in thebitflags!macro today is the lack const support in trait implementations. I've been against inventing attributes like#[no_const]that alter the behavior ofbitflags!, because complexmacro_rulesmacros are difficult to maintain. If we were to cook up a proc-macro based version of the macro though with more configurability that could cover this is that something you'd consider using?The case I'm actually looking at right now is with MMIO registers for a hardware peripheral, but it's equivalent to memory-mapped data.
A proc-macro based version of
bitflagssounds workable to me.Just as another data point: I created endian-num as a endian-aware version of
core's integer primitives for MMIO purposes (zerocopy's types have no alignment requirement). In virtio-spec-rs/src/bitflags.rs, I adaptedbitflag's macro to allow creating bitflags via const conversions to integer primitives and then doing the const operation on these primitives before converting back.This is similar to how endian-awareness was allowed in https://github.com/wrenger/bitfield-struct-rs/pull/39.
No objections to a flexible proc-macro-based version of
bitflags, but maybe allowing this via conversions might be comparatively easy?Thanks for pointing this out @mkroening. It looks like that bitfields library is already using proc macros, where adding attributes like this is more straightforward.