Skip to main content

bitfld

Attribute Macro bitfld 

Source
#[bitfld]
Expand description

§Define bit field struct or bit field enum or bit field

§Define bit enum

mark a enum can be used inside bit struct, if enum defined every value valid for repr type then impl BitsCast, otherwise impl Uncheckable , derive Clone and Copy implicitly, default #[repr(primary int of bits type)] if not add #[repr(..)] attribute

#[bitfld(u3)] // u3 is bits type, actually expand to `#[derive(BitsCast, Clone, Copy)] #[bitfld(u3)] #[repr(u8)]`
#[derive(MapEnum)] // if requires `EnumMap<BitEnum1, ..>`
enum BitEnum1 {
    A = 0,
    B = 1
}

For fieldness enum, must set tag(ty, range) and maybe set payload(ty, range) to define bits layout, range can be singlebit (like 2) or bit range (like 1..3 or 1..=2) or start:bits (like 1:2). range is optional, if omited in tag then default is start from zero, if omited in payload then default is start from tag end until total end. tag type ty must be int type but payload type ty must be unsigned int type

#[bitfld(u4, tag(u1, 0), payload(u3, 1..4))]
#[derive(MapEnum)] // if requires `EnumMap<BitEnum1, ..>`
enum BitEnum2 {
    A(u2) = 0,
    B(u3) = 1
}

§Define bit field struct, note:

  • Every field in struct will be convert to a method to get bit field ref
  • because fields will be convert to method, #[bitfld(...)] maybe needs to add before other attributes if it requires real field
  • underlying type must be unsigned basic int (u1, u2, …, u128)
  • bit field type can be any T: BitsCast type and [T; N] array and EnumMap<Enum, T> where Enum: MapEnum, includes u1, u2, …, u128, i1, i2, …, i128, bool, any bit struct and any bit enum
  • derive Clone and Copy and bytemuck::Zeroable and BitsCast, derive bytemuck::Pod and ConvertEndian or Uncheckable only if avaiable
  • default #[repr(transparent)] if not add #[repr(..)] attribute
#[bitfld(pub u32, relative)] // using #[bitfld(pub u32, .., overlay, ..)] will allow overlay for all fields, u32 is underlying type
struct BitStruct1 {
    pub a: bitfld!(bool, 1), // single bit, same as 1..2 or 1..=1 or 1:1

    pub b: bitfld!(BitEnum1, 2..=4), // same as 2..5 or 2:3

    pub c: bitfld!(u5, 2..7, overlay), // same as 2..=6 or 2:5, reuqires `overlay` here because overlay field `b`

    pub d: bitfld!(i3, :3), // occupy 3 bits, start bit use next bit of pre defined bit field, same as 7..10 or 7..=9 or 7:3 here

    pub e: bitfld!(bool, ..11), // start bit use next bit of pre defined bit field, same as 10..11 or 10..=10 or 10:1 here

    pub f: bitfld!([u2; 2], 11:4)  // same as 11..15 or 11..=14 here, array of any `BitsCast` type is allowed

    pub g: bitfld!(EnumMap<BitEnum1, u4>, 15:4) // same as 15..19 or 15..=18 here, enum map of any `BitsCast` type is allowed

    #[bitfld(19..=22)] // same as ..=22 or 19..23 or ..23 or 19:4 or :4, syntax same as bitfld!(..) without the first bit type
    pub h: EnumMap<BitEnum1, u4>, // using attribute to define bit field is also ok

    pub i: bitfld!(u9, ..), // from next bit of pre defined bit field, until end of underlying, same as 23..32 or 23..=31 or 23:9 here
}

§Define bit field

using attribute #[bitfld(range, attributes..)] or macro type bitfld!(bittype, range, attributes..) to define bit field

  • absolute range a..b or a..=b or single bit a(= a..a+1 or a..=a) or a:bits(= a..a+bits)
  • relative range, start from next bit of pre defined bit field, ..b or ..=b or :bits, requires relative in bit struct’s #[bitfld(..)]
  • relative tail range, a.. or .., occupy until underlying end, requires relative in bit struct’s #[bitfld(..)]