
Crate enum-tools
Automatically derive functions and trait implementations for enums.
The enum must be field-less, has a primitive representation and be Copy
.
Many helpful function can be derived:
- Into/TryFrom primitive
- iterator over all items and/or names
- next/previous item
- from/to string
- and more
For the full documentation see EnumTools
.
Example
#[derive(Clone, Copy, EnumTools)]
#[enum_tools(as_str, from_str, into, MAX, MIN, next, next_back, try_from)]
#[enum_tools(Debug, Display, FromStr, Into, IntoStr, TryFrom)]
#[enum_tools(iter, names, range)]
#[repr(i8)]
pub enum MyEnum { A=0, B=5, C=1 }
Derives something similar as below
impl MyEnum {
pub const MIN : Self = MyEnum::A;
pub const MAX : Self = MyEnum::B;
pub fn as_str(self) -> &'static str
pub fn from_str(s: &str) -> Option<Self>
pub const fn into(self) -> i8 { self as i8 }
pub fn next(self) -> Option<Self> pub fn next_back(self) -> Option<Self> pub fn try_from(value: i8) -> Option<Self>
pub fn iter() -> MyEnumIter pub fn names() -> MyEnumNames pub fn range(start: Self, end: Self) -> MyEnumIter }
impl Debug for MyEnum impl Display for MyEnum impl From<MyEnum> for i8 impl From<MyEnum> for &'static str impl FromStr for MyEnum
impl TryFrom<i8> for MyEnum
pub struct MyEnumIter
impl Iterator for MyEnumIter
impl DoubleEndedIterator for MyEnumIter
impl ExactSizeIterator for MyEnumIter
impl FusedIterator for MyEnumIter
pub struct MyEnumNames
impl Iterator for MyEnumNames
impl DoubleEndedIterator for MyEnumNames
impl ExactSizeIterator for MyEnumNames
impl FusedIterator for MyEnumNames