1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/*! # Enum Ordinalize This crates provides `create_ordinalized_enum` macro to let enums can not only get its variants' ordinal but also be constructed from an ordinal. ## Create an Ordinalized Enum `create_ordinalized_enum` macro can create an enum and implement a `ordinal` method, as well as `from_ordinal` and `from_ordinal_unsafe` associated functions for it. The new enum also implements `Debug`, `PartialOrd`, `Ord`, `PartialEq`, `Clone`, `Eq`, `Hash` and `Copy` traits. ```rust #[macro_use] extern crate enum_ordinalize; create_ordinalized_enum!(MyEnum, Zero, One, Two ); assert_eq!(2, MyEnum::Two.ordinal()); assert_eq!(Some(MyEnum::One), MyEnum::from_ordinal(1)); create_ordinalized_enum!(pub MyPublicEnum, A, B, C ); assert_eq!(2, MyPublicEnum::C.ordinal()); assert_eq!(Some(MyPublicEnum::B), MyPublicEnum::from_ordinal(1)); create_ordinalized_enum!(MySpecialEnum, Two = 2, Four = 4, Eight = 8 ); assert_eq!(2, MySpecialEnum::Two.ordinal()); assert_eq!(Some(MySpecialEnum::Four), MySpecialEnum::from_ordinal(4)); ``` ## About an Ordinalized Enum An ordinalized enum is sized **isize** by default. If you want to change it, just assign an integer type explicitly. ```rust #[macro_use] extern crate enum_ordinalize; create_ordinalized_enum!(MyEnum: u8, Zero, One, Two ); assert_eq!(2u8, MyEnum::Two.ordinal()); assert_eq!(Some(MyEnum::One), MyEnum::from_ordinal(1u8)); ``` If you are 100% sure that the **isize** value can transmute into a variant of your ordinalized enum. You can use the `from_ordinal_unsafe` associated function and the **unsafe** keyword to speed up. ```rust #[macro_use] extern crate enum_ordinalize; create_ordinalized_enum!(MyEnum, Zero, One, Two ); assert_eq!(MyEnum::One, unsafe{MyEnum::from_ordinal_unsafe(1)}); ``` */ mod macros;