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 #[skip] attribute for the fields of the #[bitfield] macro #42

Closed Robbepop closed 3 years ago

Robbepop commented 3 years ago

Using the proposed #[skip] attribute on bitfield fields it is possible to entirely skip code generation for the flagged field. Also this allows to have two optional parameters, getters and setters that skips generation of the field's getters or setters respectively. By default #[skip] skips both, #[skip(getters)] only skips generation of getters, #[skip(setters)] only skips generation of setters and #[skip(getters, setters)] is just equal to #[skip].

henrikssn commented 3 years ago

This would also be useful to skip compiler errors on unused fields.

Example:

#[bitfield]
pub struct MyBitfield {
  pub foo: B2,
  unused: B1,
  pub bar: B5,
}
warning: associated function is never used: `unused`
    |
338 |   unused: B1,
    |   ^^^^^^
Robbepop commented 3 years ago

As a trick it should be noted that it will be possible to use __ (double wildcard) as identifier with #[skip]:

#[bitfield]
pub struct Sparse {
    a: bool,
    #[skip] __: B10,
    b: bool,
    #[skip] __: B10,
}
Robbepop commented 3 years ago

Implemented in https://github.com/Robbepop/modular-bitfield/pull/49. Closed.