Emulating structural enums in Rust.
Cargo.toml
:
toml
[dependencies.enumx]
version = "0.3.1"
src/lib.rs
:
rust
use enumx::*;
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 =
.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 = ```rust }
``` ```rust }
``` ```rust }
``` ```rust }
``` ```rust 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
Licensed under MIT.
#[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]
.[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 ),
}
#[ty_pat(gen_variants)]
to generate missing types in Enum!()
[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(),
}
#[ty_pat(gen A,B,..)]
to generate A,B,.. etc[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(),
}
TyPat
to wrap types that are not paths, e.g. references, (), in a
#[ty_pat] match
's arm:[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 ),
}
#[derive( EnumX )]
to make user-defined enums exchangable from/to other
Enum!()
s or #[derive(EnumX)]
enums.[derive( EnumX )]
#[enumx]
closures/let-bindings and #[ty_pat] match
.License