macro_rules! enum_variants_convert {
(
$enum_vis: vis enum $enum_ident: ident
$( <[ $( $enum_gen: tt )* ]> )?
$( where [ $( $enum_bound: tt )* ] )?
{
$(
$var_ident: ident
( $var_ty: ty )
),*
$(,)?
}
) => { ... };
(@TOKENIZE
$enum_gens_tt: tt
$enum_bounds_tt: tt
$enum_vis: vis enum $enum_ident: ident {
$( $var_ident: ident ($var_ty: ty) ),*
$(,)?
}
) => { ... };
(@SINGLE
{ $( $generic: tt )* }
{ $( $bound: tt )* }
$enum_vis: vis
$enum_ident: ident
$var_ident: ident
$var_ty: ty
) => { ... };
}Expand description
§Implements for each variant’s type:
- From for Enum
- TryFrom for Variant
ENUM: Defines the input enum with its visibility, name, generics, where clauses, and variants.
§ENUM:
[enum_vis] enum [name]<[generics]> [where [bounds]] {
[var_name_A]([var_type_A]),
[var_name_B]([var_type_B]),
}
[enum_vis]: Visibility level of the enum. (e.g., pub)
[name]: Identifier (name) of the enum. (e.g., MyEnum)
[generics]: Optional generics for the enum, must be placed inside brackets. (e.g., <[T]>)
where [bounds]: Optional where clause for the enum, must be placed inside brackets. (e.g., where [T: SomeTrait])
[var_name]([var_type]): Variants of the enum along with their types. (e.g., VariantOne(TypeOne), VariantTwo(TypeTwo))
§Example
use declarative_type_state::enum_variants_convert;
#[derive(Debug, PartialEq, Eq)]
pub enum Integer {
Int(i32),
UInt(u32),
Long(i64),
ULong(u64),
}
enum_variants_convert! {
enum Integer {
Int(i32),
UInt(u32),
Long(i64),
ULong(u64),
}
}
let integer = Integer::from(5_i32);
assert_eq!(integer, Integer::Int(5));
assert_eq!(Ok(5_i32), i32::try_from(integer));