dzamlo / rust-bitfield

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

`const fn` getters #41

Open Apanatshka opened 7 months ago

Apanatshka commented 7 months ago

I'm using bitfield combined with varlen, which allows const expressions over fields to be used to compute lengths of arrays. Would it be possible to generate const fn getters for bit fields so I can use those in my computation?

dzamlo commented 7 months ago

I wasn't able to generate const fn getters because the getter call a method from the (internal) BitRange trait which is not const. I tried to make it const but it seems to not be possible to make a method from a trait const (see rustc --explain E0379).

If you find a satisfying workaround that doesn't imply getting rid of the trait, I would be happy to implement it.

Apanatshka commented 7 months ago

I read up on that explanation and the linked RFC. Looks like const traits aren't a thing (yet?). All I can think of to do is to generate your current BitRange implementations as loose pub const fns, generate the BitRange implementations to use those, and have the exposed macro's of bitfield use the const version so they can be const themselves. Then the users of the crate can choose between the const functions and the more convenient trait abstraction.