#enum #iterator #enumerate #variants #cardinality

enum-iterator

Tools to iterate over all values of a type (e.g. all variants of an enumeration)

17 releases (6 stable)

Uses new Rust 2021

1.2.0 Aug 20, 2022
1.1.3 Jul 4, 2022
1.1.2 Jun 18, 2022
0.8.1 May 7, 2022
0.2.0 Jul 2, 2018

#19 in Rust patterns

Download history 136701/week @ 2022-08-15 135527/week @ 2022-08-22 168762/week @ 2022-08-29 169136/week @ 2022-09-05 143692/week @ 2022-09-12 142118/week @ 2022-09-19 168067/week @ 2022-09-26 142749/week @ 2022-10-03 187518/week @ 2022-10-10 179615/week @ 2022-10-17 140389/week @ 2022-10-24 128566/week @ 2022-10-31 114737/week @ 2022-11-07 161850/week @ 2022-11-14 88452/week @ 2022-11-21 124793/week @ 2022-11-28

495,775 downloads per month
Used in 555 crates (81 directly)

0BSD license

29KB
529 lines

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.

Dependencies

~210–610KB
~15K SLoC