devela::all

Macro enumset

Source
macro_rules! enumset {
    (
        // $enum_attr: the attributes of the enum.
        // $enum_vis:  the visibility of the enum.
        // $enum_name: the name of the new enum.
        // $set_attr:  the attributes of the set.
        // $set_vis:   the visibility of the set.
        // $set_name:  the name of the associated set.
        // $set_ty:    the inner integer primitive type for the bitfield (u8, i32, …).
        $( #[$enum_attr:meta] )*
        $enum_vis:vis enum $enum_name:ident
            $( < $($gen:tt),* $(,)? > )? // optional generics and lifetimes
            // attributes, visibility, name and inner type of the set, between ():
            ( $( #[$set_attr:meta] )* $set_vis:vis $set_name:ident: $set_ty:ty )
            $([where $($where:tt)+ $(,)? ] $(,)? )? // optional where clauses, between []
        {
            $(
                $( #[$variant_attr:meta] )*
                $variant_name:ident
                $(( $($tuple_type:ty),* $(,)? ))?
                $({ $( $( #[$field_attr:meta] )* $field_name:ident : $field_type:ty),* $(,)? })?
                $(= $discriminant:expr)?
                $(,)?
            )*
        }
    ) => { ... };
}
Available on _bit_· only.
Expand description

Defines an enum and an associated type set of its variants.

It uses the bitfield! macro to create the associated set.

You have to give unique names both to the enum and to the associated set.

§Features

This macro depends on enabling any of the _bit features. E.g. _bit_u8.

§Examples

See also the enumset example.

enumset! {
    pub enum MyEnum(pub MyEnumSet: u8) {
        Variant1,
        Variant2(bool),
        Variant3{a: u8, b: u16},
    }
}
assert_eq![3, MyEnum::ENUM_VARIANTS];
let mut eset = MyEnumSet::default();
assert![eset.is_empty()];
eset.mut_set_field_variant1();
assert![eset.is_field_variant1()];