variant_enum

Crates.iovariant_enum
lib.rsvariant_enum
version0.1.0
sourcesrc
created_at2024-07-05 18:13:29.061245
updated_at2024-07-05 18:13:29.061245
descriptionvariant enum. generate enum variant. static dispatch.
homepage
repository
max_upload_size
id1292939
size11,460
Ycy (ycysdf)

documentation

README

variant_enum

License Crates.io Docs

Example

use variant_enum::typed_variant;

#[typed_variant(derive(Debug))]
#[derive(derive_more::From)]
pub enum Msg {
    #[inner(derive(Clone))]
    A {
        pub a: u32,
        b: f32,
        c: usize,
        d: String,
    },
    B {
        foo: String,
    },
    C {
        bar: bool,
    },
}

generated:

#[derive(Debug)]
#[derive(derive_more::From)]
pub enum Msg {
    A(A),
    B(B),
    C(C),
}
#[derive(Clone)]
#[derive(Debug)]
pub struct A {
    pub a: u32,
    b: f32,
    c: usize,
    d: String,
}
#[derive(Debug)]
pub struct B {
    foo: String,
}
#[derive(Debug)]
pub struct C {
    bar: bool,
}
impl TryFrom<Msg> for A {
    type Error = Msg;
    fn try_from(value: Msg) -> Result<Self, Self::Error> { if let Msg::A(m) = value { Ok(m) } else { Err(value) } }
}
impl TryFrom<Msg> for B {
    type Error = Msg;
    fn try_from(value: Msg) -> Result<Self, Self::Error> { if let Msg::B(m) = value { Ok(m) } else { Err(value) } }
}
impl TryFrom<Msg> for C {
    type Error = Msg;
    fn try_from(value: Msg) -> Result<Self, Self::Error> { if let Msg::C(m) = value { Ok(m) } else { Err(value) } }
}
Commit count: 0

cargo fmt