packed_struct.rs icon indicating copy to clipboard operation
packed_struct.rs copied to clipboard

Support for reserved bit with arbitrary value

Open iilyak opened this issue 3 years ago • 0 comments

Currently there is support for reserved bits sets to zero or one.

    #[packed_field(bits="0..=2")]
    _zeroes: ReservedZero<packed_bits::Bits::<3>>,
    #[packed_field(bits="3..=4")]
    _reserved_one: ReservedOne<packed_bits::Bits::<2>>,

However there is no (it seems) way to define a constant value for reserved bits.

I found two approaches to define it.

  1. use constructor
#[derive(PackedStruct, Debug, Clone, PartialEq)]
#[packed_struct(bit_numbering = "msb0", endian = "msb")]
pub struct Status {
    #[packed_field(bits = "0..=3")] // bit numbering is MSB
    _start_flag: Integer<u8, packed_bits::Bits<4>>, // MUST be 0b1100

....
pub const STATUS_FLAG: u8 = 0xA0;
impl Status {
    pub fn new() -> Status {
        Status {
            _start_flag: STATUS_FLAG.into(),
        }
    }
}
  1. define dummy fields for each bit (which makes constructor bigger)
pub struct Status {
     // next four bits represent a constant 0b1100
     _start_flag3: ReservedOne<packed_bits::Bits::<1>>,
     _start_flag2: ReservedOne<packed_bits::Bits::<1>>,
     _start_flag1: ReservedZero<packed_bits::Bits::<1>>,
     _start_flag0: ReservedZero<packed_bits::Bits::<1>>,

It would be amazing if I could pass a constant somehow (ReservedBits<0b1100, packet_bits::Bits<4>>?)

iilyak avatar May 01 '22 06:05 iilyak