Macro enumap::enumap

source ·
macro_rules! enumap {
    (
        $(#[$($attr:tt)*])*
        $vis:vis enum $name:ident {
            $(
                $(#[$($vattr:tt)*])*
                $v:ident
            ),* $(,)?
        }
    ) => { ... };
}
Expand description

Lightweight macro to automatically implement Enum.

The macro automatically implements Enum, it works only on data less enums and automatically derives Copy and Clone.

§Example:

use enumap::EnumMap;

enumap::enumap! {
    /// A beautiful fruit, ready to be sold.
    #[derive(Debug, PartialEq, Eq)]
    enum Fruit {
        Orange,
        Banana,
        Grape,
    }
}

// A fruit shop: fruit -> stock.
let mut shop = EnumMap::new();
shop.insert(Fruit::Orange, 100);
shop.insert(Fruit::Banana, 200);
shop.insert(Fruit::Grape, 300);

assert_eq!(
    shop.into_iter().collect::<Vec<_>>(),
    vec![(Fruit::Orange, 100), (Fruit::Banana, 200), (Fruit::Grape, 300)],
);

// Oranges out of stock:
shop.remove(Fruit::Orange);

assert_eq!(
    shop.into_iter().collect::<Vec<_>>(),
    vec![(Fruit::Banana, 200), (Fruit::Grape, 300)],
);