packed_struct.rs
packed_struct.rs copied to clipboard
C structures in rust
Hello I am trying to translate my C code into rust. I am using your crate to get bitfields support. When I do sth like this:
#[derive(Debug, PartialEq, Eq, PackedStruct)]
#[packed_struct(bit_numbering="msb0")]
pub struct ModulesStatusPackage{
#[packed_field(bits="0:1")]
cpu : u16,
#[packed_field(bits="2:3")]
relays : u16,
#[packed_field(bits="4:5")]
imu : u16,
#[packed_field(bits="6:7")]
label : u16,
#[packed_field(bits="8:9")]
cam_switch : u16,
#[packed_field(bits="10:11")]
science : u16,
#[packed_field(bits="12:13")]
unused : u16,
#[packed_field(bits="14:15")]
bridge_drive_0 : u16,
#[packed_field(bits="16:17")]
bridge_drive_1 : u16,
#[packed_field(bits="18:19")]
bridge_drive_2 : u16,
#[packed_field(bits="20:21")]
bridge_drive_3 : u16,
#[packed_field(bits="22:23")]
bridge_mani_0 : u16,
#[packed_field(bits="24:25")]
bridge_mani_1 : u16,
#[packed_field(bits="26:27")]
bridge_mani_2 : u16,
#[packed_field(bits="28:29")]
bridge_mani_3 : u16,
}
I am getting errors like this:
error[E0277]: the trait bound `packed_struct::types::Integer<u16, packed_struct::types::bits::Bits2>: std::convert::From<u16>` is not satisfied
--> src\main.rs:8:32
|
8 | #[derive(Debug, PartialEq, Eq, PackedStruct)]
| ^^^^^^^^^^^^ the trait `std::convert::From<u16>` is not implemented for `packed_struct::types::Integer<u16, packed_struct::types::bits::Bits2>`
|
= help: the following implementations were found:
<packed_struct::types::Integer<i16, packed_struct::types::bits::Bits10> as std::convert::From<i16>>
<packed_struct::types::Integer<i16, packed_struct::types::bits::Bits11> as std::convert::From<i16>>
<packed_struct::types::Integer<i16, packed_struct::types::bits::Bits12> as std::convert::From<i16>>
<packed_struct::types::Integer<i16, packed_struct::types::bits::Bits13> as std::convert::From<i16>>
and 124 others
= note: required because of the requirements on the impl of `std::convert::Into<packed_struct::types::Integer<u16, packed_struct::types::bits::Bits2>>` for `u16`
= note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
I was looking at the other issues but I could not find any helpfull information.
u16 cannot be packed and unpacked from 2 bits, you need to wrap it with a type that fits into there. Following the example from the main README file, use this as the type for your fields:
Integer<u8, packed_bits::Bits2>