Derive Macro integral_enum::IntegralEnum

source ·
#[derive(IntegralEnum)]
{
    // Attributes available to this derive:
    #[enum_disable]
}
Expand description

Macro that implements bunch of traits for enums that simply are aliases for integer type

example:

use integral_enum::IntegralEnum;

#[derive(IntegralEnum)]
#[repr(u8)]
// repr is not required, by default `IntegralEnum` assumes that repr is u8
// FIXME: repr should be infered to avoid specific errors
pub enum Yuu {
    Hatred,
    Pain,
}

assert_eq!(Yuu::try_from(0), Ok(Yuu::Hatred));
assert_eq!(Yuu::try_from(1), Ok(Yuu::Pain));
assert_eq!(Yuu::try_from(123), Err(()));

Macro will automatically generate such trait implementations: Clone, Copy, PartialEq, Eq, Debug, Display, TryFrom.

To resolve conflicts with other derive-macro you should use the #[enum_disable(…)] attribute, it has the following options:

debug, display, clone, copy, partial_eq, total_eq, try_from

total_eq depends on partial_eq, copy depends on clone, so if you disable dependency, dependant implementation will be disabled too, for example:

use integral_enum::IntegralEnum;

#[derive(Debug, IntegralEnum)]
#[enum_disable(debug)]
enum Affection {
    FellInLove,
    HatesYou,
    // From love to hate is one step, after all :D
}

assert_eq!(format!("{:?}", Affection::FellInLove), "FellInLove");
assert_eq!(format!("{}", Affection::HatesYou), "HatesYou");
// display is still available