hecatia-elegua / bilge

Use bitsized types as if they were a feature of rust.
Apache License 2.0
174 stars 18 forks source link

Figure out multi-bit set/get for bitflags-like usage #19

Open hecatia-elegua opened 1 year ago

hecatia-elegua commented 1 year ago

Not entirely sure what a good design might look like here.

dicta commented 1 year ago

I am currently working around this not being implemented yet by converting a uN to its underlying storage representation (u8, u32, etc.), using the trait implementation from bit_field here: BitField, and then repacking the uN.

The linked crate/trait was the most-readable-to-code-using-it API design I had found to date for integrating bitflags-like things into existing numeric types. I don't love its panic behaviour, but otherwise, I'd use it if it integrated with types provided by arbitrary-int.

hecatia-elegua commented 1 year ago

@dicta Thank you for the info!

I guess I'll think about it now: I'm inclined to expand sth like

//...
struct Foo {
   field1: u3,
   #[flags(status)]
   field2: bool,
   field3: bool,
   field4: bool,
   field5: u2,
   field6: bool,
}

to all their field getters and setters plus

fn status() -> u3
fn set_status(u3)

and then consts for these three fields (like in bitflags, so you can | them together). We could use BitField too, but I'm trying to avoid user code which looks like set_bit(1), since it won't tell you anything at first glance.

I'm open to bikeshedding and also to just being proven wrong.