#variant #enums #map #store #struct #macro-derive

variant-map

defines maps to store variants of enums

2 releases

0.1.1 Sep 6, 2023
0.1.0 Sep 6, 2023

#1456 in Data structures

35 downloads per month

MIT license

19KB
390 lines

variant-map

Enum variants stored in Maps.

Provides different kinds of map-equivalent types to store enum variants into. As those data structures are maps, they store one value of each variant. All Maps are serde::Serialize-able and serde::Deserialize-able

Those maps can be generated easily using the derive macros from variant_map_derive. variant_map_derive provides a derive macro for a StructMap (a struct type with a field per enum variant). variant_map_derive can be included using the derive feature of variant_map

This crate also provide simple macros to lighten the syntax with the macros feature.

Example

    use variant_map_derive::VariantStore;

    #[derive(VariantStore)]
    enum MyEnum {
        A,
        B(i32),
    }

    fn main() {
        use variant_map::{as_key, as_map};
        let mut map = <as_map!(MyEnum)>::default();
        let _: &MyEnum = map.get(&<as_key!(MyEnum)>::A).unwrap();
        let _: &MyEnum = map.get(&MyEnumKey::A).unwrap();
        map[&MyEnumKey::B] = MyEnum::B(69);
    }

For more customizability of the Map check out the [variant_map_derive] crate documentation

For more detailed examples check out the example project on this crates' repo

Dependencies

~0.5–1.1MB
~25K SLoC