#enums #variant #iterator #cardinality #enumerate

enum-iterator

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

24 releases (13 stable)

2.1.0 May 1, 2024
2.0.0 Feb 25, 2024
1.5.0 Jan 15, 2024
1.4.1 May 4, 2023
0.2.0 Jul 2, 2018

#28 in Rust patterns

Download history 161826/week @ 2024-07-30 177613/week @ 2024-08-06 150544/week @ 2024-08-13 171846/week @ 2024-08-20 159743/week @ 2024-08-27 161073/week @ 2024-09-03 147586/week @ 2024-09-10 132559/week @ 2024-09-17 152275/week @ 2024-09-24 170409/week @ 2024-10-01 145349/week @ 2024-10-08 175098/week @ 2024-10-15 160325/week @ 2024-10-22 165488/week @ 2024-10-29 149198/week @ 2024-11-05 172529/week @ 2024-11-12

672,048 downloads per month
Used in 1,399 crates (161 directly)

0BSD license

34KB
628 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

~205–630KB
~15K SLoC