A library for to allow multiple return types by automatically generated enum.
This crate is a procedural macro implementation of the features discussions in [rust-lang/rfcs#2414]. This idea is also known as "Anonymous sum types".
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.7"
Compiler support: requires rustc 1.31+
#[auto_enum]
's basic feature is to wrap the value returned by the obvious
branches (match
, if
, return
, etc..) by an enum that implemented the
specified traits.
```rust use autoenums::autoenum;
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
#[auto_enum]
can also parse nested arms/branches by using the #[nested]
attribute.
```rust use autoenums::autoenum;
fn foo(x: i32) -> impl Iterator
See documentation for more details.
#[enum_derive]
implements the supported traits and passes unsupported
traits to #[derive]
.
#[enum_derive]
supports many of the standard library traits and some popular
third-party libraries traits such as [rayon], futures,
tokio. See documentation for a complete list of supported traits.
If you want to use traits that are not supported by #[enum_derive]
, you
can use another crate that provides derives macros, or
you can define derives macros yourself ([derive_utils] probably can help it).
Basic usage of #[enum_derive]
```rust use autoenums::enumderive;
// #[enum_derive]
implements Iterator
, and #[derive]
implements Clone
.
enum Foo { A(A), B(B), } ```
std
(enabled by default)
std
library's traits.ops
[std|core]::ops
's Deref
, DerefMut
, Index
, IndexMut
, and RangeBounds
traits.convert
[std|core]::convert
's AsRef
and AsMut
traits.fmt
[std|core]::fmt
's traits other than Debug
, Display
and Write
.transpose_methods
transpose*
methods.futures03
futures01
rayon
serde
tokio1
tokio03
tokio02
tokio01
generator_trait
[std|core]::ops::Generator
trait.fn_traits
[std|core]::ops
's Fn
, FnMut
, and FnOnce
traits.trusted_len
[std|core]::iter::TrustedLen
trait.type_analysis
featureAnalyze return type of function and let
binding.
Note that this feature is still experimental.
Examples:
```rust use autoenums::autoenum;
fn func1(x: i32) -> impl Iterator
fn func2(x: i32) {
// Unlike feature(impl_trait_in_bindings)
, this works on stable compilers.
#[autoenum]
let _iter: impl Iterator
Please be careful if you return another traits with the same name.
Licensed under either of Apache License, Version 2.0 or MIT license 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.