dzamlo / rust-bitfield

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

Unable to "Debug" for types that are backed by an array >32bytes #28

Closed ctron closed 4 years ago

ctron commented 4 years ago

Having a structure which is backed by an array of bytes that is greater than 32 bytes fails:

error[E0277]: arrays only have std trait implementations for lengths 0..=32
  --> examples/simple.rs:70:38
   |
70 |     log::info!("Calibration: {:#?}", cal);
   |                                      ^^^ the trait `core::array::LengthAtMost32` is not implemented for `[u8; 41]`
   |
   = note: required because of the requirements on the impl of `core::convert::AsRef<[u8]>` for `[u8; 41]`
   = note: required because of the requirements on the impl of `core::fmt::Debug` for `drogue_bme680::Coefficients<[u8; 41]>`
   = note: required by `core::fmt::Debug::fmt`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.

I think there should be a way to prevent the automatic Debug generation to debug self.0, which should do the trick.

dzamlo commented 4 years ago

Testing quickly locally, it's seems to works. Could you share the definitions of drogue_bme680::Coefficients and the call to bitfield!?

No Debug implementation is generated by default. You need to add impl Debug (like https://github.com/dzamlo/rust-bitfield/blob/d7d9d3b2cfac226ab5d29d9d6a641caff7dbdf8b/tests/lib.rs#L598 for exemple)

ctron commented 4 years ago

Yes, that is true. But did you try to actually print it?

Here is a simple reproducer:

use bitfield::bitfield;

bitfield! {
    pub struct Test([u8]);
    impl Debug;
}

fn main() {
    let buffer = [0u8;41];
    let test = Test(buffer);
    println!("{:?}", test);
}
dzamlo commented 4 years ago

This works on the nightly and beta toolchains, but not on stable. The current beta will become stable next week if I can count weeks correctly.

I will not implement support for not including .0 in the Debug implementation but I will gladly accept a merge request if you think this is really useful.

ctron commented 4 years ago

If this works next week, that would be fine for me. I would prefer solving a problem by waiting a few days :grin:

brandonros commented 1 year ago

What would it take to support defmt::Format so this can be used in an embedded context?

https://docs.rs/defmt/latest/defmt/trait.Format.html

Or maybe I'm using it wrong?