Crate enum_iterator

Source
Expand description

§Overview

Tools to iterate over the values of a type.

§Examples

use enum_iterator::{all, cardinality, first, last, next, previous, reverse_all, Sequence};

#[derive(Debug, PartialEq, Sequence)]
enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }

assert_eq!(cardinality::<Day>(), 7);
assert_eq!(all::<Day>().collect::<Vec<_>>(), [
    Day::Monday,
    Day::Tuesday,
    Day::Wednesday,
    Day::Thursday,
    Day::Friday,
    Day::Saturday,
    Day::Sunday,
]);
assert_eq!(first::<Day>(), Some(Day::Monday));
assert_eq!(last::<Day>(), Some(Day::Sunday));
assert_eq!(next(&Day::Tuesday), Some(Day::Wednesday));
assert_eq!(previous(&Day::Wednesday), Some(Day::Tuesday));
assert_eq!(reverse_all::<Day>().collect::<Vec<_>>(), [
    Day::Sunday,
    Day::Saturday,
    Day::Friday,
    Day::Thursday,
    Day::Wednesday,
    Day::Tuesday,
    Day::Monday,
]);
use enum_iterator::{cardinality, first, last, Sequence};

#[derive(Debug, PartialEq, Sequence)]
struct Foo {
    a: bool,
    b: u8,
}

assert_eq!(cardinality::<Foo>(), 512);
assert_eq!(first::<Foo>(), Some(Foo { a: false, b: 0 }));
assert_eq!(last::<Foo>(), Some(Foo { a: true, b: 255 }));

§Rust version

This crate tracks stable Rust. Minor releases may require a newer Rust version. Patch releases must not require a newer Rust version.

§Contribute

All contributions shall be licensed under the 0BSD license.

Structs§

All
Iterator over the values of type T.
ReverseAll
Iterator over the values of type T in reverse order.

Traits§

Sequence
Trait to iterate over the values of a type.

Functions§

all
Returns an iterator over all values of type T.
cardinality
Returns the cardinality (number of values) of T
first
Returns the first value of type T.
last
Returns the last value of type T.
next
Returns the next value of type T or None if this was the end.
next_cycle
Returns the next value of type T or first() if this was the end.
previous
Returns the previous value of type T or None if this was the beginning.
previous_cycle
Returns the previous value of type T or last() if this was the beginning.
reverse_all
Returns an iterator over all values of type T in the reverse order of all.

Derive Macros§

Sequence
Derives Sequence.