Attribute Macro enumber::convert[][src]

Expand description

Convert an enumber compliant enum into a proper enum with appropriate associated traits.

As an example, you might have the following enum definition:

enum Ordinals {
    First = 1,
    Second = 2,
    Third = 3,

Normally that enum would be invalid because of the combination of both explicit discriminants, and a tuple-style variant. The conversion will strip the discriminants off the enumeration and then implement conversions to/from the number type of the tuple-style variant.

In addition, implementations of Display and FromStr will be automatically provided. The error type for the FromStr implementation will be named Parse${NAME}Error and will have the same visibility specifier as your enum had.

The error enumeration will look like this:

enum ParseOrdinalsError {

Naturally the integer type in the UnknownValue variant will be that of the enum’s tuple-style variant.

If you do not wish to have an “other” variant, then you can omit it, in which case you must specify the representation of the enumeration explicitly:

enum AccessLevel {
    Guest = 0,
    Member = 1,
    Developer = 2,
    Owner = 3,

In this case, From<${TYPE}> will not be implemented, and instead there will be an implementation of TryFrom<${TYPE}> where the error type for that conversion is simply the input number type.

You may specify variants as taking a range rather than a fixed value, and if you do, then the variant will be converted into a tuple type automatically, in order to hold the exact value given during conversion. As with the above examples, if you do not also specify a default variant then you will have to specify the relevant integer representation.

Finally, if you are certain that your given values (or ranges) cover all possible input values to the conversion functions, and you wish to omit the default variant, then you can specify #[exhaustive] on the enumeration and enumber will create a From<${TYPE}> impl despite the lack of the default variant. Of course, if your values are not exhaustive then the compiler will flag an error and not continue.

enum Age {
    Child = 0..=12,
    Teenager = 13..=19,
    Adult = 20..=65,
    Pensioner = 66..=254, // Not quite enough, so this will fail to compile