auto_enums
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 traits received from
#[auto_enum]
.
Usage
Add this to your Cargo.toml
:
[]
= "0.1"
and this to your crate root:
extern crate auto_enums;
Examples
#[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.
// generats an enum with two variants
You can also use #[auto_enum]
for expressions and statements.
use ;
#[auto_enum]
has several other features. See API Documentation for more details.
Supported traits
#[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.
Basic usage of #[enum_derive]
// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.
[std|core] libraries
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
) - note that it is a different implementation from#[derive(Debug)]
.Display
(alias:fmt::Display
)fmt::Binary
fmt::LowerExp
fmt::LowerHex
fmt::Octal
fmt::Pointer
fmt::UpperExp
fmt::UpperHex
fmt::Write
[std|core]::future
Future
- nightly-only
std::io
std::error
External libraries
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)
serde::Serialize
- note that it is a different implementation from#[derive(Serialize)]
.
Static methods
These don't derive traits, but derive static methods instead.
-
Transpose
(requires"transpose_methods"
crate feature) - this derives the following conversion methods.-
transpose
- convert fromenum<Option<T1>,..>
toOption<enum<T1,..>>
-
transpose
- convert fromenum<Result<T1, E1>,..>
toResult<enum<T1,..>, enum<E1,..>>
-
transpose_ok
- convert fromenum<Result<T1, E>,..>
toOption<enum<T1,..>, E>
Examples:
use ;
-
transpose_err
- convert fromenum<Result<T, E1>,..>
toResult<T, enum<E1,..>>
-
Known limitations
-
There needs to explicitly specify the trait to be implemented (
type_analysis
crate feature reduces this limitation). -
There needs to be marker macros for expressions other than
match
andif
.
Rust Version
The current minimum required Rust version is 1.30.
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
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.