Feature Request: Explicit names and values for enum variants
Like the title says, allowing for explicit naming and values.
For explicit naming it has some drawbacks as to doing it the old and easy way:
bounded_integer! {
#[repr(u8)]
pub enum Freqency { 0..3 }
}
But the consumer has to work with values such as Freqency::Z0 and Freqency::P1 which in some cases is not descriptive enough. I might want to name the variants in this way:
bounded_integer! {
#[repr(u8)]
pub enum Freqency {
TwoHz,
OneHz,
HalfHz
}
}
In this example I have the enum variants sent to a circuit with hardcoded values via an i2c interface.
If explicit naming is implemented we lose the ability to set the range of the bounded enum so explicit values needs to be implemented with it.
With explicit values there is the possabilities that the consumer of this library makes "holes" in the bounded range.
bounded_integer! {
#[repr(u8)]
pub enum Freqency {
TwoHz = 0b0000_0001,
HalfHz = 0b0000_0011,
}
}
If we for some reason happends to find ourself in one of the holes (possible with the step trait) we can treat the hole as an out of bounds exception.
For this use case, what would be the benefit in using this library at all? You are able to write:
#[repr(u8)]
pub enum Freqency {
TwoHz = 0,
OneHz,
HalfHz
}
in normal Rust, and OneHz and HalfHz will automatically be assigned the values 1 and 2. You can use #[derive(FromPrimitive)] to automate the u8 -> Frequency conversion. Is there a particular feature you need from this library?
There is the step feature which Im intressted in using. It's for a library and I want to keep things as open and accessable as possible. Plus this library implements a lot of arithmetical operations which is great for creating display patterns in a safe inbounds way.
Hmm, ok. I don't think I want to support enums with holes, it feels out of scope, but I can definitely implement custom enum variant names.
Thanks! How will the syntax look? And how will the range be set?
I'm thinking you will just write out enum variants like in a regular Rust enum. The range will be set implicitly by the range of the values of the enum (erroring if there are holes).
Like so:
bounded_integer! {
#[repr(u8)]
enum Type {
Variant0,
Variant1
}
}
and so:
bounded_integer! {
#[repr(u8)]
enum Type {
Variant0 = 21,
Variant1 = 22
}
}
?
And is the error a compile time error ?
I'm not sure about the first one, since no starting value is explicitly specified - I might require Variant0 = 0. It will be a compile time error.