33 releases

Uses old Rust 2015

0.12.0 Jul 5, 2023
0.11.0 Nov 16, 2022
0.10.0 Nov 6, 2022
0.9.12 Mar 13, 2022
0.1.0 Feb 5, 2017

#988 in Rust patterns

Download history 6/week @ 2024-09-17 71/week @ 2024-09-24

135 downloads per month
Used in vampire_api

MIT/Apache

21KB
460 lines

This crate offers some tools to deal with static enums. It offers a way to declare a simple enum, which then offers e.g. values() which can be used to iterate over the values of the enum. In addition, it offers a type EnumMap which is an array-backed map from enum values to some type.

It offers a macro plain_enum_mod which declares an own module which contains a simple enum and the associated functionality:

mod examples_not_to_be_used_by_clients {
    #[macro_use]
    use plain_enum::*;
    plain_enum_mod!{example_mod_name, ExampleEnum {
        V1,
        V2,
        SomeOtherValue,
        LastValue, // note trailing comma
    }}
    
    fn do_some_stuff() {
        let map = ExampleEnum::map_from_fn(|example| // create a map from ExampleEnum to usize
            example.to_usize() + 1                   // enum values convertible to usize
        );
        for ex in ExampleEnum::values() {            // iterating over the enum's values
            assert_eq!(map[ex], ex.to_usize() + 1);
        }
    }
}

Internally, the macro generates a simple enum whose numeric values start counting at 0.

No runtime deps