Announcing `derive(FromVariant)` for enum

ast_node is a proc macro with more feature, but I think derive(FromVariant) is useful even for projects other than swc.

Note:

It's a part of the swc project, but you don't need to depend on swc to use derive(FromVariant).

Usage

https://github.com/kdy1/rust-from-variant-test

Cargo.toml:

[dependencies]
ast_node = "0.1.2"

main.rs:

extern crate ast_node;

use ast_node::FromVariant;

pub struct Foo {}
pub struct Bar {}


#[derive(FromVariant)]
pub enum Example {
    Foo(Foo),
    Bar(Bar),
}

fn main() {
    Example::from(Foo {});
    Example::from(Bar {});
}
5 Likes