ordinalize_enum
macro can implement a ordinal
method and a from_ordinal
associated function for an existing enum
.
```rust
enum MyEnum { Zero, One, Two, }
ordinalize_enum!(MyEnum, u8, Zero, One, Two );
asserteq!(2, MyEnum::Two.ordinal()); asserteq!(Some(MyEnum::One), MyEnum::from_ordinal(1)); ```
create_ordinalized_enum
macro can create an enum and implement a ordinal
method and a from_ordinal
associated function for it.
The new enum also implements Debug
, PartialEq
, and Clone
traits.
```rust
createordinalizedenum!(MyEnum, u8, Zero, One, Two );
asserteq!(2, MyEnum::Two.ordinal()); asserteq!(Some(MyEnum::One), MyEnum::from_ordinal(1));
createordinalizedenum!(pub MyPublicEnum, u8, A, B, C );
asserteq!(2, MyPublicEnum::C.ordinal()); asserteq!(Some(MyPublicEnum::B), MyPublicEnum::from_ordinal(1));
createordinalizedenum!(MySpecialEnum, u8, Two = 2, Four = 4, Eight = 8 );
asserteq!(2, MySpecialEnum::Two.ordinal()); asserteq!(Some(MySpecialEnum::Four), MySpecialEnum::from_ordinal(4)); ```
https://crates.io/crates/enum-ordinalize
https://docs.rs/enum-ordinalize