dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

Feature request: Right-padded fields #37

Open mumblingdrunkard opened 10 months ago

mumblingdrunkard commented 10 months ago

Somewhat related to #36

I'm creating a RISC-V emulator and the instruction formats sometimes encode values in the instructions, called immediates. Sometimes, the instruction only contains the upper few bits of the immediate value. E.g.: image Here, only the upper 20 bits are included in the instruction, from bit 31 to 12. and the lower 12 bits of the immediate should be set to 0. I imagine syntax similar to the one proposed in #36 but with literal values that should occupy some number of bits in the field; e.g.:

imm, _: [(31, 12), 12(0)];
// alternatively
imm, _: 31, 12, 12(0);

It does raise the question of how the setter works when the field has padding. I imagine it should just mask away the padded section, or simply panic when there are non-zero bits in the padded section.

It shouldn't be too difficult to support internal padding, but I can't imagine a good use case for that.

I understand that this is probably reaching outside the scope of what this package is meant to do, but it would certainly make my code a lot cleaner :).