pub trait IntoEnumIterator: Sized {
    type Iterator: Iterator<Item = Self> + Clone;

    const ITEM_COUNT: usize;

    fn into_enum_iter() -> Self::Iterator;
}
Expand description

Trait to iterate over the values of a type.

Derivation

IntoEnumIterator can be derived for enum and struct types. Specifically, it can be derived for:

  • Enumerations whose variants meet one of the following criteria:
    • The variant does not have fields.
    • The variant has fields such that:
      • Every field has a type that implements IntoEnumIterator.
      • Every field but the last one has a type that implements Clone.
  • Structures whose fields are such that:
    • Every field has a type that implements IntoEnumIterator.
    • Every field but the last one has a type that implements Clone.

The number of values of the type must not exceed usize::MAX.

Examples

C-like enumeration

use enum_iterator::IntoEnumIterator;

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum Direction { North, South, West, East }

assert_eq!(Direction::ITEM_COUNT, 4);
assert!(Direction::into_enum_iter().eq([
    Direction::North,
    Direction::South,
    Direction::West,
    Direction::East,
]));

Enumeration with data

use enum_iterator::IntoEnumIterator;

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum Direction { North, South, West, East }

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum Greeting {
    Hi,
    Bye,
}

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum Action {
    Move(Direction),
    Jump,
    Talk { greeting: Greeting, loud: bool },
}

assert_eq!(Action::ITEM_COUNT, 4 + 1 + 2 * 2);
assert!(Action::into_enum_iter().eq([
    Action::Move(Direction::North),
    Action::Move(Direction::South),
    Action::Move(Direction::West),
    Action::Move(Direction::East),
    Action::Jump,
    Action::Talk { greeting: Greeting::Hi, loud: false },
    Action::Talk { greeting: Greeting::Hi, loud: true },
    Action::Talk { greeting: Greeting::Bye, loud: false },
    Action::Talk { greeting: Greeting::Bye, loud: true },
]));

Structure

use enum_iterator::IntoEnumIterator;

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum Side {
    Left,
    Right,
}

#[derive(Clone, Copy, IntoEnumIterator, PartialEq)]
enum LimbKind {
    Arm,
    Leg,
}

#[derive(IntoEnumIterator, PartialEq)]
struct Limb {
    kind: LimbKind,
    side: Side,
}

assert_eq!(Limb::ITEM_COUNT, 4);
assert!(Limb::into_enum_iter().eq([
    Limb { kind: LimbKind::Arm, side: Side::Left },
    Limb { kind: LimbKind::Arm, side: Side::Right },
    Limb { kind: LimbKind::Leg, side: Side::Left },
    Limb { kind: LimbKind::Leg, side: Side::Right },
]));

Required Associated Types

Type of the iterator returned by IntoEnumIterator::into_enum_iter.

Required Associated Constants

Number of values in Self.

Required Methods

Returns an iterator over the values of Self.

Implementations on Foreign Types

Implementors