Robbepop / modular-bitfield

Macro to generate bitfields for structs that allow for modular use of enums.
Apache License 2.0
155 stars 40 forks source link

Add initial support for setting endian with BitfieldSpecifier. #67

Open bramp opened 3 years ago

bramp commented 3 years ago

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.

Robbepop commented 3 years ago

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?

bramp commented 3 years ago

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 commented 3 years ago

@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

vhdirk commented 1 year ago

@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.