Robbepop / modular-bitfield

Macro to generate bitfields for structs that allow for modular use of enums.
Apache License 2.0
155 stars 40 forks source link

Feature request: Array support #76

Open VisualEhrmanntraut opened 2 years ago

VisualEhrmanntraut commented 2 years ago

Currently, the following is not supported:

#[bitfield]
pub struct Testing {
    #[skip] __: [bool; 512],
}

It will complain with the error below:

error[E0277]: the trait bound `[bool; 512]: modular_bitfield::Specifier` is not satisfied
   --> src/test.rs:41:5
    |
41  |     #[skip] __: [bool; 512],
    |     ^^^^^^^^^^^^^^^^^^^^^^^ the trait `modular_bitfield::Specifier` is not implemented for `[bool; 512]`
    |
note: required by `modular_bitfield::Specifier::BITS`
   --> /Users/visual/.cargo/registry/src/github.com-1ecc6299db9ec823/modular-bitfield-0.11.2/src/lib.rs:455:5
    |
455 |     const BITS: usize;
    |     ^^^^^^^^^^^^^^^^^^
bracketttc commented 2 years ago

So, I want this as well, but I wonder if it's even possible. I tried:

impl moduler_bitfield::Specifier for [B5; 5] {
// ...
}

only to get

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> src/main.rs:66:1
   |
66 | impl Specifier for [B5; 5] 
   | ^^^^^^^^^^^^^^^^^^^-------
   | |                  |
   | |                  this is not defined in the current crate because arrays are always foreign
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

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

which seems like you can't add a trait to an array?

VisualEhrmanntraut commented 2 years ago

So, I want this as well, but I wonder if it's even possible.

I tried:


impl moduler_bitfield::Specifier for [B5; 5] {

// ...

}

only to get


error[E0117]: only traits defined in the current crate can be implemented for arbitrary types

  --> src/main.rs:66:1

   |

66 | impl Specifier for [B5; 5] 

   | ^^^^^^^^^^^^^^^^^^^-------

   | |                  |

   | |                  this is not defined in the current crate because arrays are always foreign

   | impl doesn't use only types from inside the current crate

   |

   = note: define and implement a trait or new type instead

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

which seems like you can't add a trait to an array?

What it means is that you'll have to implement the trait inside of the modular_bitfield crate I assume to add such array support that we will have to add to the the trait implementation generation new logic for arrays, perhaps by parsing the array syntax, seeing its element type and count, then handling data representation accordingly?

miathedev commented 1 year ago

Any Updates to this issue?