plain_enum 0.12.0

Mimicing Java's enum::values() and EnumMap
Documentation

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.