Motivation

Emulating structural enums in Rust.

Usage

  1. Add this crate to Cargo.toml

Cargo.toml:

toml [dependencies.enumx] version = "0.3.1"

src/lib.rs:

rust use enumx::*;

  1. Use Enum!(A,B,..) to define a structural enum, the variants of which are A, B, .. etc.

```rust let a: Enum!(i32,String) = 42.into_enum();

let b = variant( "the answer".toowned() ); ```

  1. Use .from_enumx()/.into_enumx() to convert between structural enums and/or their variants.

```rust let c: Enum!(i32,String,bool) = 42.into_enumx();

let d: Enum!(i32,String,bool) = a.into_enumx();

let e =

let f =

  1. Use #[ty_pat] match to do pattern matching against an Enum!(A, B, ..), the arms of which are not variants but types A, B, .. etc. The fn containing the match expression must be tagged #[enumx].

```rust

[enumx] fn foo( input: Enum!(String,i32) ) {

#[ty_pat] match input {
    String(s) => println!( "it's string:{}", s ),
    i32(i) => println!( "it's i32:{}", i ),
}

} ```

```rust

[enumx] fn foo( input: Enum!(String,i32) ) -> Enum!(String,i32) {

#[ty_pat(gen_variants)] match input {
    i32(i) => (i+1).into_enumx(),
    // generated arm: String(s) => s.into_enumx(),
}

} ```

```rust

[enumx] fn foo( input: Enum!(String,i32) ) -> Enum!(String,i32) {

#[ty_pat(gen String)] match input {
    i32(i) => (i+1).into_enumx(),
    // generated arm: String(s) => s.into_enumx(),
}

} ```

```rust

[enumx] fn bar( input: Enum!(&'static str,i32) ) {

#[ty_pat] match input {
    TyPat::<&'static str>(s) => println!( "it's static str:{}", s ),
    i32(i) => println!( "it's i32:{}", i ),
}

} ```

  1. Use #[derive( EnumX )] to make user-defined enums exchangable from/to other Enum!()s or #[derive(EnumX)] enums.

```rust

[derive( EnumX )]

enum Info { Code(i32), Text(&'static str), }

let info: Info = 42.into_enum();

let a: Enum!(&'static str, i32) = "a".into_enum();

let info: Info = a.into_enumx();

let b: Enum!(&'static str, i32) = info.into_enumx(); ```

Notice

All the features provided by this crate work with stable Rust, including #[enumx] closures/let-bindings and #[ty_pat] match.

License

Licensed under MIT.