Robbepop / modular-bitfield

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

Allow skipping into/from bytes and new functions #56

Open T-Bakker opened 3 years ago

T-Bakker commented 3 years ago

Hi,

I currently use bitfields to write bytes to and/or read bytes from a data bus. As some bitfields are only received while others are only sent, the bitfields cause dead_code warnings. Using #[allow(dead_code)] does not seem to have an effect.

As such, I would like it to be possible to skip the generation of these functions, much like it is possible to disable the byte boundry or to specify the size of the bitfield, like as such:

#[bitfield(skip_from_bytes)]
pub struct MeOnlySent {
}

#[bitfield(skip_into_bytes, skip_new)]
pub struct MeOnlyReceived {
}

#[bitfield(skip_new)]
pub struct MeOnlyForwardedAndModified {
}

If this option is already available somewhere or it is possible to avoid these warnings in some other manner, please give some direction. I could not find it.

Robbepop commented 3 years ago

Sounds like a good idea. Not yet implemented though.

T-Bakker commented 3 years ago

Please note that this could also be extended into skipping all getters/setters for a bitfield, thus avoiding having to specify this for every member of the bitfield, like as such:

#[bitfield(skip_from_bytes, skip_getters)]
pub struct MeOnlySent {
}

#[bitfield(skip_into_bytes, skip_new, skip_setters)]
pub struct MeOnlyReceived {
}

#[bitfield(skip_new)]
pub struct MeOnlyForwardedAndModified {
}

I am not sure if this is desired as it makes the API a bit more complex while reducing the number of lines for defining a bitfield. Especially when working with large (32bit or even 64bit) bitfields, this could be quite significant. Up to you to decide if this is desired :).

Robbepop commented 3 years ago

I think a proper syntax for this could be something like:

#[bitfield(skip(from_bytes, getters))]
pub struct MyBitfield {}
T-Bakker commented 3 years ago

Yes, I agree. My syntax was an example to get the idea across :).

lkolbly commented 3 years ago

@T-Bakker @Robbepop I opened #58, I was able to make the dead_code warnings go away I think but for each of the from_bytes and into_bytes methods there were multiple places I had to add it to make the warning go away. (there's both expand_byte_conversion_impls and ::modular_bitfield::Specifier)

lkolbly commented 3 years ago

If you don't have #[skip(getters)] specified on a bitfield, would the getters that it generates still be generated with allow(dead_code)?

A user could conceivably only use getters for some fields.

mehmetalianil commented 2 years ago

This seems to be settled on, but I wondered today whether the scope of skip could be extended to the whole struct. I mean some structs that I define are all read only registers, I can make that explicit and skip all the setters for those. The initial proposal could have been useful that way.