Motivation
Emulating structural enums in Rust.
Usage
- Add this crate to Cargo.toml
Cargo.toml:
[]
= "0.3.1"
src/lib.rs:
use *;
- Use
Enum!(A,B,..)to define a structural enum, the variants of which are A, B, .. etc.
let a: Enum! = 42.into_enum;
let b = >from_variant;
- Use
.from_enumx()/.into_enumx()to convert between structural enums and/or their variants.
let c: Enum! = 42.into_enumx;
let d: Enum! = a.into_enumx;
let e = >from_enumx;
let f = >from_enumx;
- Use
#[ty_pat] matchto do pattern matching against anEnum!(A, B, ..), the arms of which are not variants but types A, B, .. etc. Thefncontaining the match expression must be tagged#[enumx].
- Use
#[ty_pat(gen_variants)]to generate missing types inEnum!()
!
- Use
#[ty_pat(gen A,B,..)]to generate A,B,.. etc
!
- Use
TyPatto wrap types that are not paths, e.g. references, (), in a#[ty_pat] match's arm:
- Use
#[derive( EnumX )]to make user-defined enums exchangable from/to otherEnum!()s or#[derive(EnumX)]enums.
let info: Info = 42.into_enum;
let a: Enum! = "a".into_enum;
let info: Info = a.into_enumx;
let b: Enum! = 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.