dzamlo / rust-bitfield

This crate provides macros to generate bitfield-like struct.
Apache License 2.0
157 stars 19 forks source link

Requires AsMut and AsRef with buffers > 32 bytes #29

Closed ctron closed 4 years ago

ctron commented 4 years ago

I think this is similar to the issue #21:

Assume the following code:

extern crate bitfield;
use bitfield::bitfield;

pub const SIZE: usize = 33;

bitfield! {
    pub struct Foo(MSB0[u8]);
    impl Debug;

    pub u8, foo, _ : 0;
}

fn main() {
    let buffer = [0u8; SIZE];
    Foo(buffer).foo();
}

This will result in:

error[E0599]: no method named `foo` found for struct `Foo<[u8; 33]>` in the current scope
  --> src/main.rs:16:17
   |
7  | / bitfield! {
8  | |     pub struct Foo(MSB0[u8]);
9  | |     impl Debug;
10 | |
11 | |     pub u8, foo, _ : 0;
12 | | }
   | |_- method `foo` not found for this
...
16 |       Foo(buffer).foo();
   |                   ^^^ method not found in `Foo<[u8; 33]>`
   |
   = note: the method `foo` exists but the following trait bounds were not satisfied:
           `[u8; 33]: std::convert::AsRef<[u8]>`
           `[u8; 33]: std::convert::AsMut<[u8]>`

If you lower the size to "32" (or less), it works.

ctron commented 4 years ago

You can work around this using the following code:

let mut foo = Foo(buffer);
let foo = Foo(&mut foo.0[..]);
foo.foo();

But this requires to have a mut instance, even for "read-only" operations.

dzamlo commented 4 years ago

Like #28, this is fixed in the beta toolchain.

But this requires to have a mut instance, even for "read-only" operations.

This part of the issue is the same as #23