This project is a proof of concept of implementing anonymous enum in Rust via proc-macro.
If you are seeking for error handling, perhaps cex
crate is more suitable for you.
Anonymous enums composed of x variants are named as Enum1
, Enum2
, ... etc.
Variants are named as _0
, _1
, ... etc.
Each variant type in an EnumX
is convertable to this EnumX
.
An EnumX
T is convertable to another EnumX
U if all variants in T are in U.
Inside a function with #[enumx]
attribute and returning EnumX
, the code can return any type that is convertable to the function return type.
However, explicit .into()
are required.
A function returning an anonymous enum composed of 2 variants.
```rust
fn f2( variantindex: usize ) -> Enum2
asserteq!( f2(0), Enum2::0( "runtime error".tostring() )); asserteq!( f2(1), Enum2::_1( 0xdead )); ```
Some 2-variants EnumX
that is convertable to 3-variants EnumX
.
```rust
fn f3( variantindex: usize ) -> Enum3
Variants' order does not matter.
```rust
fn g3( variantindex: usize ) -> Enum3
Those who are not interested in anonymous enum can work with the libs that using anonymouse enum.
rust
fn h( variant_index: usize ) -> String {
match f3( variant_index ) {
Enum3::_0( string ) => string.into(),
Enum3::_1( errno ) => format!( "errno: 0x{:x}", errno ),
Enum3::_2( flag ) => format!( "flag: {}", flag ),
}
}
assert_eq!( h(0), "runtime error".to_string() );
assert_eq!( h(1), "errno: 0xdead".to_string() );
assert_eq!( h(2), "flag: false".to_string() );
The library users should use concrete types as the type parameters of EnumX, rather than generics.
This restriction may be removed in the upcoming version 0.2.0.
DO NOT use nested EnumX
and expect it be flatterned.
Currently this project does nothing to support flatterning.
For example,Enum2<i32,usize>
cannot be converted to Enum2<Enum2<i32,String>,usize>
in #[enumx] fn
.
Current version supports up to 32 variants in a EnumX
.
In otherwords, Enum1
..=Enum32
is available but Enum33
but the succeeding enums are not.
If it is proved that more than 32 variants are useful in practice, they may be supported in later version.
Notice that the compile time will grow in O( n*n )
impl
s.
Some sort of identifiers are reserved for implementation usage.
enumx
/enum_derive
crates reserve identifiers starting with __EnumX
, __enumx
and the identifier LR
.
Licensed under MIT.