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

Overlap in bits where there is non

Open wucke13 opened this issue 5 years ago • 3 comments

This code

#[derive(PackedStruct)]
#[packed_struct(bit_numbering="msb0")]
pub struct PsMpf {
    #[packed_field(bytes="0", bits="2")]
    _reserved_h_2: ReservedOne<BitOne>,
    #[packed_field(bytes="0", bits="3")]
    _reserved_h_3: ReservedZero<BitOne>,
    #[packed_field(bytes="0", bits="4")]
    _reserved_h_4: ReservedOne<BitOne>,
    #[packed_field(bytes="0", bits="5")]
    _reserved_h_5: ReservedZero<BitOne>,
    #[packed_field(bytes="0", bits="6")]
    _reserved_h_6: ReservedOne<BitOne>,
    #[packed_field(bytes="0", bits="7")]
    _reserved_h_7: ReservedZero<BitOne>,
}

results in this error:

error: Overlap in bits between fields _reserved_h_2 and _reserved_h_3
 --> src/types.rs:8:10
  |
8 | #[derive(PackedStruct)]
  |          ^^^^^^^^^^^^
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

error: could not compile `demonstrator`

To learn more, run the command again with --verbose.

Why is that?

wucke13 avatar Dec 03 '20 15:12 wucke13

bytes and bits both specify the same thing, and it might be that bytes has priority here. Can you try it without bytes?

This should also be an attribute-level error to prevent both attributes from being used at the same time.

rudib avatar Dec 03 '20 15:12 rudib

Yes, removing the bytes fixes that error, but new errors occure which I do not really understand. I would really like to be able to combine bit and byte. In this case the structures length is at least 27 bytes, id rather not have something like bit="219"...

The new error:

error[E0599]: no method named `pack` found for reference `&ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>` in the current scope
  --> src/types.rs:8:10
   |
8  | #[derive(PackedStruct)]
   |          ^^^^^^^^^^^^ method not found in `&ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>`
   |
  ::: /home/wucke13/.cargo/registry/src/github.com-1ecc6299db9ec823/packed_struct-0.4.0/src/types_reserved.rs:19:1
   |
19 | pub struct BitOne;
   | ------------------ doesn't satisfy `packed_struct::types::BitOne: NumberOfBits`
...
38 | pub struct ReservedBits<V, B> {
   | ----------------------------- doesn't satisfy `_: packed_struct::PackedStruct`
   |
   = note: the method `pack` exists but the following trait bounds were not satisfied:
           `packed_struct::types::BitOne: NumberOfBits`
           which is required by `ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>: packed_struct::PackedStruct`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `pack` found for reference `&ReservedBits<BitZero, packed_struct::types::BitOne>` in the current scope
  --> src/types.rs:8:10
   |
8  | #[derive(PackedStruct)]
   |          ^^^^^^^^^^^^ method not found in `&ReservedBits<BitZero, packed_struct::types::BitOne>`
   |
  ::: /home/wucke13/.cargo/registry/src/github.com-1ecc6299db9ec823/packed_struct-0.4.0/src/types_reserved.rs:19:1
   |
19 | pub struct BitOne;
   | ------------------ doesn't satisfy `packed_struct::types::BitOne: NumberOfBits`
...
38 | pub struct ReservedBits<V, B> {
   | ----------------------------- doesn't satisfy `_: packed_struct::PackedStruct`
   |
   = note: the method `pack` exists but the following trait bounds were not satisfied:
           `packed_struct::types::BitOne: NumberOfBits`
           which is required by `ReservedBits<BitZero, packed_struct::types::BitOne>: packed_struct::PackedStruct`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no function or associated item named `unpack` found for struct `ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>` in the current scope
  --> src/types.rs:8:10
   |
8  | #[derive(PackedStruct)]
   |          ^^^^^^^^^^^^ function or associated item not found in `ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>`
   |
  ::: /home/wucke13/.cargo/registry/src/github.com-1ecc6299db9ec823/packed_struct-0.4.0/src/types_reserved.rs:19:1
   |
19 | pub struct BitOne;
   | ------------------ doesn't satisfy `packed_struct::types::BitOne: NumberOfBits`
...
38 | pub struct ReservedBits<V, B> {
   | ----------------------------- doesn't satisfy `_: packed_struct::PackedStruct`
   |
   = note: the method `unpack` exists but the following trait bounds were not satisfied:
           `packed_struct::types::BitOne: NumberOfBits`
           which is required by `ReservedBits<packed_struct::types::BitOne, packed_struct::types::BitOne>: packed_struct::PackedStruct`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no function or associated item named `unpack` found for struct `ReservedBits<BitZero, packed_struct::types::BitOne>` in the current scope
  --> src/types.rs:8:10
   |
8  | #[derive(PackedStruct)]
   |          ^^^^^^^^^^^^ function or associated item not found in `ReservedBits<BitZero, packed_struct::types::BitOne>`
   |
  ::: /home/wucke13/.cargo/registry/src/github.com-1ecc6299db9ec823/packed_struct-0.4.0/src/types_reserved.rs:19:1
   |
19 | pub struct BitOne;
   | ------------------ doesn't satisfy `packed_struct::types::BitOne: NumberOfBits`
...
38 | pub struct ReservedBits<V, B> {
   | ----------------------------- doesn't satisfy `_: packed_struct::PackedStruct`
   |
   = note: the method `unpack` exists but the following trait bounds were not satisfied:
           `packed_struct::types::BitOne: NumberOfBits`
           which is required by `ReservedBits<BitZero, packed_struct::types::BitOne>: packed_struct::PackedStruct`
   = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0599`.
error: could not compile `demonstrator`

To learn more, run the command again with --verbose.

wucke13 avatar Dec 03 '20 18:12 wucke13

BitOne is not the right type to use here. packed_bits::Bits1 would be the correct one. See the example at https://github.com/hashmismatch/packed_struct.rs/blob/master/packed_struct_tests/tests/packing_reserved.rs#L9

bits and bytes can be used interchangeably between fields, just not both on the same field, as they overwrite each other! Also, in your example here, you can simply just define the first field's position, then the subsequent ones will follow it directly.

#[derive(PackedStruct)]
#[packed_struct(bit_numbering="msb0")]
pub struct PsMpf {
    #[packed_field(bits="2")]
    _reserved_h_2: ReservedOne<packed_bits::Bits1>,
    _reserved_h_3: ReservedZero<packed_bits::Bits1>,
    _reserved_h_4: ReservedOne<packed_bits::Bits1>,
    _reserved_h_5: ReservedZero<packed_bits::Bits1>,
    _reserved_h_6: ReservedOne<packed_bits::Bits1>,
    _reserved_h_7: ReservedZero<packed_bits::Bits1>
}

rudib avatar Dec 03 '20 18:12 rudib