[][src]Crate tyenum

Attribute macro for less verbose creation of enums having different types as variants.

Also automatically implements From, TryFrom and fn is<T>() -> bool to check if its inner item is of type T .

Usage:

struct A;
struct B;
struct C;

#[tyenum]
enum Test {
    A,
    BB(B),
    C(C),
}

results in:

enum Test {
    A(A),
    BB(B),
    C(C),
}

impl Test {
    fn is<T: IsTypeOf<Test>>(&self) -> bool {
        T::is_type_of(self)
    }
}

and for every variant (in this example A):

impl From<A> for Test {
    fn from(variant: A) -> Self {
        Test::A(variant)
    }
}

impl TryFrom<Test> for A {
    type Error = TryFromTyenumError;
    fn try_from(e: Test) -> Result<Self, TryFromTyenumError> {
        if let Test::A(variant) = e {
            Ok(variant)
        } else {
            Err(TryFromTyenumError)
        }
    }
}

impl IsTypeOf<Test> for A {
    fn is_type_of(e: &Test) -> bool {
        if let Test::A(_) = e {
            true
        } else {
            false
        }
    }
}