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 constant/magic value #86

Open apbr opened 1 year ago

apbr commented 1 year ago

Add the possibility to define some values as constant/magic values. Such a value is always the same. It can't be changed in the constructor and also has no setter.

Possible syntax:

#[bitsize(13)]
struct Register {
    header: u4,
    body: u7,
   #[magic(Code::Error)]
    code: Code,
}

#[bitsize(2)]
#[derive(FromBits)]
enum Code { Success, Error, IoError, GoodExample }
hecatia-elegua commented 11 months ago

There are various concepts called magic number. File header magic numbers could be excluded when parsing. If the number is needed afterwards, like in your example supposedly, it would just need a fixed value for code and read-only values I think.

apbr commented 10 months ago

If the number is needed afterwards, like in your example supposedly, it would just need a fixed value for code and read-only values I think.

Yes, that's what I am looking for, so is this already possible with bilge?