modular-bitfield icon indicating copy to clipboard operation
modular-bitfield copied to clipboard

Add initial support for setting endian with BitfieldSpecifier.

Open bramp opened this issue 3 years ago • 4 comments

Hi,

I'm a beginner with rust (this is day 4). I very much like how easy it was to parse binary data with this library. I however needed some Endian support.

I have this working on the BitfieldSpecifier macro. For example:

#[derive(BitfieldSpecifier, Debug)]
#[bits = 16]
#[endian = 1]
pub enum QClass {
    Reserved = 0,   // [RFC6895]
    Internet = 1,   // (IN)   [RFC1035]
    // ...
}

#[bitfield(bits = 32)] // Excluding the variable length qname
#[derive(Copy, Clone, Debug)]
pub struct Question {
    qtype: u16,  // TODO enum 0x0001 A record
    qclass: QClass,
}

Notice the #[endian = 1] it changes the BitfieldSpecifier to pass things though to_be and from_be.

Does this approach make sense? If so, I'd like to clean it up, add tests, write docs, and extend it to bitfield.

bramp avatar May 26 '21 04:05 bramp

Hey @bramp,

thanks a ton for the PR! We were having a long discussion about exactly this feature here: https://github.com/Robbepop/modular-bitfield/issues/46 (Sorry for the bad discoverability ...)

As you will find out it is not easy to come up with a solution that fits all. However, we do need a solution since many people really would like to properly handle endianess through this crate.

Can you tell why you chose #[endian = 1] instead of something like #[endian = "little"] or so?

Robbepop avatar May 26 '21 09:05 Robbepop

I'll read though that thread. Thanks.

As for #[endian = 1] that was because I don't know how to put a enum there :) I left myself a TODO to figure it out... Really the intent of this PR was to get something that works, to start a discussion. I didn't want to polish something that would be rejected.

bramp avatar May 26 '21 15:05 bramp

@Robbepop if you could give me the thumbs up, I'll clean this up, add docs/tests/whatever. I'll start with BitfieldSpecifier then move to Bitfield

bramp avatar May 27 '21 04:05 bramp

@bramp @Robbepop I'm a bit late to the party here, but I was wondering if there had been any progress on this PR in the meantime. I'm willing to donate some time to get this finished. However, I'm not solely interested in enums. For my use-case, I need absolute bit-by-bit equality to C bitfields.

vhdirk avatar Nov 25 '22 21:11 vhdirk