hecatia-elegua / bilge

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

Add DefaultBits, overwriting Default to fix its semantics #61

Closed hecatia-elegua closed 1 year ago

hecatia-elegua commented 1 year ago

Closes #50. Taking @pickx example to explain this again, but slightly different:

#[bitsize(1)]
#[derive(Default, FromBits)]
enum HasDefault {
    A, 
    #[default]
    B
}

#[bitsize(1)]
#[derive(Default, FromBits)]
struct Foo {
    has_def: HasDefault
}

Here, HasDefault::default() would be 1. But Foo is expanded like this:

#[derive(Default)]
struct Foo { value: u1 };

which will generate Foo { value: 0 } as the default, which is wrong. Since it is completely wrong, we will now just silently change Default to be DefaultBits.

I feel like DebugBits should do the same. Or not abort. You either want deep debugging with DebugBits or just want some number telling you the internal state, which Debug would still do.