Skip to content

Support for reserved bit with arbitrary value #91

Open
@iilyak

Description

@iilyak

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>>?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions