ShiftEnum

Derive Macro ShiftEnum 

Source
#[derive(ShiftEnum)]
Expand description

This derive macro will implement next() and prev() methods that shifts the variant to the annotated enum.

  • next() will return Some(Variant) where Variant is next one in the enum, or None if it was the last variant of the enum.
  • prev() will return Some(Variant) where Variant is previous one in the enum, or None if it was the first variant of the enum.

For code examples, see module-level docs.

§Requirements

  • It must be applied to an enum. Structs are not supported or won’t make sense.
  • Enums with any associated data are not supported.

§Generated methods

For example, this macro will implement functions like below for enum Direction.

impl Direction {
    fn next(self) -> Option<Self> {
        match self {
            Self::Up => Some(Self::Left),
            Self::Left => Some(Self::Down),
            Self::Down => Some(Self::Right),
            Self::Right => None,
        }
    }

    fn prev(self) -> Option<Self> {
        match self {
            Self::Up => None,
            Self::Left => Some(Self::Up),
            Self::Down => Some(Self::Left),
            Self::Right => Some(Self::Down),
        }
    }
}