integral-enum 1.1.2

Derive macro for integral enum definition
Documentation

Integral enum

Rust derive-macro that simplifies integral enum creation

Example

use integral_enum::IntegralEnum;

/// Simple meaningful enum
/// (!) repr attribute is required
/// and can contain only integral values
/// like u8, u16, u32, u64, u128 and its signed equivalent
#[derive(IntegralEnum)]
#[repr(u8)]
pub enum Emotion {
    // Explicit discriminant is required
    Pain = 0,
    Hatred = 1,
}

// Test it
assert_eq!(Emotion::try_from(0), Ok(Emotion::Pain));
assert_eq!(Emotion::try_from(1), Ok(Emotion::Hatred));
assert_eq!(Emotion::try_from(123), Err(()));

Macro will automatically generate the following trait implementations: TryFrom, Clone, Copy, PartialEq, Eq, Display, Debug

if you want to remove certain trait implementation, then pass one of the following attributes to the macro: #[no_debug], #[no_display], #[no_copy], #[no_total_eq], #[no_clone]

  • Some of these attributes are dependant, for example Copy depends on Clone, so if you disable Clone generation, Copy generation will be disabled also.
  • no_total_eq disables Eq trait implementation

Example:

use integral_enum::IntegralEnum;

#[derive(IntegralEnum)]
#[repr(u8)]
#[no_display]
pub enum Emotion {
    // Explicit discriminant is required
    Pain = 0,
    Hatred = 1,
}

println!("{}", Emotion::Pain); // Compile error

TODO

  • Do we really need to implement PartialOrd + Ord also?