Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C structures in rust #51

Open
mikart143 opened this issue Nov 2, 2020 · 1 comment
Open

C structures in rust #51

mikart143 opened this issue Nov 2, 2020 · 1 comment

Comments

@mikart143
Copy link

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.

@rudib
Copy link
Member

rudib commented Nov 4, 2020

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants