Variation
A procedural macro to generate enum to variant conversion methods.
Methods generated
is_*
methods
An is_variant
method is generated for each for variant in an enum.
use Variation;
as_*
& as_*_mut
methods
Variants that have one or more inner types have as
and as_mut
allowing you
to get a immutable or mutable reference to the inner types. Variants with a
single inner type will return &{mut} T
. Variants that have more than one inner
type will return a tuple with a reference to each type.
use Variation;
into_*
methods
Variants that have one or more inner types have an into
method, allowing you
to attempt to convert a enum into its inner values. This method will panic when
called on a variant that does not match the method.
use variation::Variation;
#[derive(Variation)]
enum Type {
Unit,
Integer(i32),
Real(i32, u32),
}
fn main() {
let mut return_type = Type::Integer(5);
let real_value = Type::Real(3, 14);
let unit = Type::Unit;
assert_eq!(5, return_type.into_integer());
assert_eq!((3, 14), real_value.into_real());
// Panics
unit.into_integer();
}