Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Explicit names and values for enum variants #9

Open
nilscript opened this issue May 30, 2021 · 7 comments
Open

Feature Request: Explicit names and values for enum variants #9

nilscript opened this issue May 30, 2021 · 7 comments
Labels
enhancement New feature or request

Comments

@nilscript
Copy link

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.

@Kestrer
Copy link
Owner

Kestrer commented May 30, 2021

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?

@nilscript
Copy link
Author

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.

@Kestrer
Copy link
Owner

Kestrer commented May 30, 2021

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.

@Kestrer Kestrer added the enhancement New feature or request label May 30, 2021
@nilscript
Copy link
Author

Thanks! How will the syntax look? And how will the range be set?

@Kestrer
Copy link
Owner

Kestrer commented May 31, 2021

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).

@nilscript
Copy link
Author

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 ?

@Kestrer
Copy link
Owner

Kestrer commented May 31, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants