IterEnum

Derive Macro IterEnum 

Source
#[derive(IterEnum)]
Expand description

This derive macro will implement iter() method to the annotated enum that sequentially yield the 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.
  • Enum also needs to derive Clone.

§Generated methods

For example, this macro will implement an iterator and methods like below for enum Direction.

struct DirectionIterator(Option<Direction>);

impl Iterator for DirectionIterator {
    type Item = Direction;
    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.0.clone();
        self.0 = match self.0 {
            Some(Direction::Up) => Some(Direction::Left),
            Some(Direction::Left) => Some(Direction::Down),
            Some(Direction::Down) => Some(Direction::Right),
            Some(Direction::Right) => None,
            None => None,
        };
        ret
    }
}