[][src]Crate plain_enum

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.

Macros

enum_seq_len
internal_impl_plainenum
plain_enum_mod
tt

Structs

EnumMap

Traits

TInternalEnumMapType

Trait used to associated enum with EnumMap. Needed because of https://github.com/rust-lang/rust/issues/46969. TODO Rust: Once this is solved, use array directly within EnumMap.

TPlainEnum

This trait is implemented by enums declared via the plain_enum_mod macro. Do not implement it yourself, but use this macro.