enum_iterator

Trait IntoEnumIterator

Source
pub trait IntoEnumIterator: Sized {
    type Iterator: Iterator<Item = Self> + ExactSizeIterator + FusedIterator + Copy;

    const VARIANT_COUNT: usize;

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

Trait to iterate over the variants of a field-less enum.

Field-less (a.k.a. C-like) enums are enums whose variants don’t have additional data.

This trait is meant to be derived.

§Example

use enum_iterator::IntoEnumIterator;

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

fn main() {
    assert_eq!(Direction::VARIANT_COUNT, 4);
    assert!(Direction::into_enum_iter().eq([Direction::North,
        Direction::South, Direction::West, Direction::East].iter()
        .cloned()));
}

Required Associated Constants§

Source

const VARIANT_COUNT: usize

Number of variants.

Required Associated Types§

Source

type Iterator: Iterator<Item = Self> + ExactSizeIterator + FusedIterator + Copy

Type of the iterator over the variants.

Required Methods§

Source

fn into_enum_iter() -> Self::Iterator

Returns an iterator over the variants.

Variants are yielded in the order they are defined in the enum.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§