packed_struct.rs
                                
                                 packed_struct.rs copied to clipboard
                                
                                    packed_struct.rs copied to clipboard
                            
                            
                            
                        Porting a packed C++ struct with bitfields
Hi, Thank you for your help! I am trying to read a packed struct from file created in a CPP app. This is the original struct: `struct HeaderStruct { uint16_t _t : 3;
uint16_t _p : 1;
uint16_t _l : 12;
uint16_t _s : 2;
uint16_t _q : 14;
} attribute((packed)); `
This is how I tried to implement it using your crate: ` #[derive(PackedStruct)] #[packed_struct(bit_numbering="msb0")] struct HeaderStruct { #[packed_field(bits="0..=2")] _t: Integer<u16, packed_bits::Bits::<3>>,
#[packed_field(bits="3..=3")]
_p: Integer<u16, packed_bits::Bits::<1>>,
#[packed_field(bits="4..=15", endian="msb")]
_l: Integer<u16, packed_bits::Bits::<12>>,
#[packed_field(bits="16..=17")]
_s: Integer<u16, packed_bits::Bits::<2>>,
#[packed_field(bits="18..=31", endian="msb")]
_q: Integer<u16, packed_bits::Bits::<14>>,
}`
in main what I tried to do is:
fn main() { ... let mut hdr_buffer = [0u8; 4]; file.read(&mut hdr_buffer).unwrap(); let hdr = HeaderStruct::unpack(&hdr_buffer); }
I get the following error:
error[E0599]: the method
packexists for reference&packed_struct::types::MsbInteger<_, _, packed_struct::types::Integer<u8, packed_struct::types::bits::Bits<12_usize>>>, but its trait bounds were not satisfied --> src/main.rs:21:10 | 21 | #[derive(PackedStruct)] | ^^^^^^^^^^^^ method cannot be called on&packed_struct::types::MsbInteger<_, _, packed_struct::types::Integer<u8, packed_struct::types::bits::Bits<12_usize>>>due to unsatisfied trait bounds | ::: /Users/amitaihandler/.cargo/registry/src/github.com-1ecc6299db9ec823/packed_struct-0.10.1/src/types_num.rs:14:1 | 14 | pub struct Integer<T, B> { | ------------------------ doesn't satisfy_: SizedInteger<_, _>... 571 | pub struct MsbInteger<T, B, I>(I, PhantomData<T>, PhantomData<B>); | ------------------------------------------------------------------ doesn't satisfy_: packed_struct::PackedStruct| = note: the following trait bounds were not satisfied:packed_struct::types::Integer<u8, packed_struct::types::bits::Bits<12_usize>>: SizedInteger<_, _>which is required bypacked_struct::types::MsbInteger<_, _, packed_struct::types::Integer<u8, packed_struct::types::bits::Bits<12_usize>>>: packed_struct::PackedStruct= note: this error originates in the derive macroPackedStruct(in Nightly builds, run with -Z macro-backtrace for more info)
If I understand correctly you cannot unpack a 2 bit into a 16 bit? How can I keep the struct size whilst reading from file? Sorry if this question seems trivial... thank you for any help you can offer