macro_rules! define_enum_with_introspection {
    (
        $(#![$macro_attr:ident])?
        $(#[$emeta:meta])*
        $vis:vis enum $enum:ident {
            $(
                $(#[$varmeta:meta])*
                $variant:ident $(= $discriminant:expr)?
            ),+
            $(,)?
        }
    ) => { ... };
}
Expand description

Auto-generate enum with some introspection facilities, including conversion from integers.

It automatically derives/implements the following traits:

It also defines an inherent impl for the enum.

Example

tarantool::define_enum_with_introspection! {
    pub enum MyEnum {
        A, B, C
    }
}

This macro expands into something like this:

pub enum MyEnum {
    A, B, C
}

impl MyEnum {
    pub const VARIANTS: &[Self] = &[Self::A, Self::B, Self::C];
    pub const MIN: Self = Self::A;
    pub const MAX: Self = Self::C;

    pub const fn variant_name(&self) -> &'static str {
        match self {
            Self::A => "A",
            Self::B => "B",
            Self::C => "C",
        }
    }

    pub const fn from_i64(n: i64) -> Option<Self> {
        if n <= Self::MIN as i64 || n > Self::MAX as i64 {
            return None;
        }

        Some(unsafe { std::mem::transmute(n as u8) })
    }

    // ... for full list see the implementation.
}

NOTE: currently when determining the MIN & MAX constants the enum’s variants are cast to i64, which means that discriminants with values larger than i64::MAX will give incorrect results.