A library for to allow multiple return types by automatically generated enum.
This library provides the following attribute macros:
#[auto_enum]
Parses syntax, creates the enum, inserts variants, and passes specified traits to #[enum_derive]
.
#[enum_derive]
Implements specified traits to the enum.
Add this to your Cargo.toml
:
toml
[dependencies]
auto_enums = "0.3"
and this to your crate root:
```rust
extern crate auto_enums; ```
#[auto_enum]
's basic feature is to wrap the value returned by the last if or match expression by an enum that implemented the specified traits.
```rust
fn foo(x: i32) -> impl Iterator
#[auto_enum]
generates code in two stages.
First, #[auto_enum]
will do the following.
Code like this will be generated:
```rust
fn foo(x: i32) -> impl Iterator
match x {
0 => __Enum1::__T1(1..10),
_ => __Enum1::__T2(vec![5, 10].into_iter()),
}
} ```
Next, #[enum_derive]
implements the specified traits.
Code like this will be generated
See API Documentation for more details.
#[enum_derive]
implements the supported traits and passes unsupported traits to #[derive]
.
If you want to use traits that are not supported by #[enum_derive]
, you can use another crate that provides proc_macro_derive
, or you can define proc_macro_derive
yourself ([derive_utils] probably can help it).
Basic usage of #[enum_derive]
``rust
//
#[enum_derive]implements
Iterator, and
#[derive]implements
Clone`.
enum Foo { A(A), B(B), } ```
Note that some traits have aliases.
[std|core]::ops
Deref
DerefMut
Index
IndexMut
Fn
(nightly-only)FnMut
(nightly-only)FnOnce
(nightly-only)RangeBounds
[std|core]::convert
[std|core]::iter
Iterator
DoubleEndedIterator
ExactSizeIterator
FusedIterator
TrustedLen
(nightly-only)Extend
[std|core]::fmt
Debug
(alias: fmt::Debug
) - generated codeDisplay
(alias: fmt::Display
)fmt::Binary
(requires "fmt"
crate feature)fmt::LowerExp
(requires "fmt"
crate feature)fmt::LowerHex
(requires "fmt"
crate feature)fmt::Octal
(requires "fmt"
crate feature)fmt::Pointer
(requires "fmt"
crate feature)fmt::UpperExp
(requires "fmt"
crate feature)fmt::UpperHex
(requires "fmt"
crate feature)fmt::Write
[std|core]::future
Future
- nightly-onlystd::io
std::error
You can add support for external library by activating the each crate feature.
futures(v0.3)
(requires "futures"
crate feature)
futures(v0.1)
(requires "futures01"
crate feature)
quote
(requires "proc_macro"
crate feature)
rayon
(requires "rayon"
crate feature)
serde
(requires "serde"
crate feature)
The current minimum required Rust version is 1.30.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.