Purpose
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.
Naming
Anonymous enums composed of x variants are named as Enum1
, Enum2
, ... etc.
Variants are named as _0
, _1
, ... etc.
type converting rules:
-
Each variant type in an
EnumX
is convertable to thisEnumX
. -
An
EnumX
T is convertable to anotherEnumX
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.
Examples
-
A function returning an anonymous enum composed of 2 variants.
assert_eq!; assert_eq!;
-
Some 2-variants
EnumX
that is convertable to 3-variantsEnumX
.assert_eq!; assert_eq!; assert_eq!;
-
Variants' order does not matter.
assert_eq!; assert_eq!; assert_eq!;
-
Those who are not interested in anonymous enum can work with the libs that using anonymouse enum.
assert_eq!; assert_eq!; assert_eq!;
Limitation
-
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 toEnum2<Enum2<i32,String>,usize>
in#[enumx] fn
. -
Current version supports up to 32 variants in a
EnumX
.In otherwords,
Enum1
..=Enum32
is available butEnum33
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 identifierLR
.
License
Licensed under MIT.