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]
.
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.
#
#
# extern crate auto_enumerate;
// generats an enum with two variants
#
You can also use #[auto_enum]
for expressions and statements.
#
# extern crate auto_enumerate;
use ;
#
Expression level marker (marker!
macro)
#[auto_enum]
replaces marker!
macros with variants.
#
#
# extern crate auto_enumerate;
// generats an enum with three variants
#
Also, if values of two or more are specified by marker!
macros, #[auto_enum]
can be used for a expression or statement that does not end with a if or match expression.
#
#
# extern crate auto_enumerate;
#
The default name of the macro is "marker"
, but you can change it by marker
option.
#
#
# extern crate auto_enumerate;
#
Expression that no value will be returned
If the last expression of a branch is one of the following, it is interpreted that no value will be returned (variant assignment is skipped).
panic!(..)
unreachable!(..)
return
break
continue
None?
Err(..)?
- Expression level marker (
marker!
macro). - An item definition.
#
#
# extern crate auto_enumerate;
#
You can also skip that branch explicitly by #[never]
attribute.
#
#
# extern crate auto_enumerate;
#
You can also skip all branches by never
option.
#
#
# extern crate auto_enumerate;
#
Blocks and unsafe blocks
Blocks and unsafe blocks are parsed recursively. However, empty blocks are handled in the same as other expressions.
#
#
# extern crate auto_enumerate;
#
Parse nested branches
You can parse nested branches by #[rec]
attribute.
#
#
# extern crate auto_enumerate;
#
Rust Nightly
When using #[auto_enum]
for expressions and statements, #[auto_enum]
for function is unnecessary.
#
// Add this to your crate root:
#
#
#
#
#
# extern crate auto_enumerate;
#
#
#
#
#
You can also return closures.
#
// Add this to your crate root:
#
#
#
#
# extern crate auto_enumerate;
#
#
#
#
#
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]
#
#
# extern crate auto_enumerate;
// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.
#
#[enum_derive]
adds the dependency of the specified trait if it is not specified.
#
#
#
# extern crate auto_enumerate;
// `#[enum_derive]` implements `Iterator` and `ExactSizeIterator`.
#
[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
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:
#
#
# extern crate auto_enumerate;
#
use ;
#
#
#
#
#
transpose_err
- convert fromenum<Result<T, E1>,..>
toResult<T, enum<E1,..>>
Crate Features
-
std
-
Enabled by default.
-
Disable to use
no_std
instead. -
type_analysis
-
Disabled by default.
-
Analyze return type of function and
let
binding.
Examples:
#
#
#
# extern crate auto_enumerate;
#
// there is no need to specify std library's traits
#
#
#
#
Please be careful if you return another traits with the same name.
-
transpose_methods
-
Disabled by default.
-
Use
transpose*
methods. -
unstable
-
Disabled by default.
-
Use unstable features to make attribute macros more effective.
-
The traits supported by
#[enum_derive]
are not related to this feature. -
This requires Rust Nightly.
Using external libraries (disabled by default)
-
futures
- futures(v0.3) -
futures01
- futures(v0.1) -
proc_macro
- quote -
rayon
- rayon -
serde
- serde
Enable unstable features of [std|core] libraries (disabled by default, nightly-only)
For these features, you need to enable the unstable feature gate of the same name.
-
exact_size_is_empty
- ImplementsExactSizeIterator::is_empty
. -
read_initializer
- Implementsio::Read::read_initializer
. -
try_trait
- Make iterator implementation more effective. -
unsized_locals
- AllowIndex<Idx: ?Sized>
andIndexMut<Idx: ?Sized>
.
Generated code
There are two steps to generating code.
When using #[auto_enum]
for function like the following:
#
#
# extern crate auto_enumerate;
+ Clone
#
First, #[auto_enum]
will do the following.
- parses syntax
- creates the enum
- inserts variants
Code like this will be generated:
#
#
# extern crate auto_enumerate;
+ Clone
#
Next, #[enum_derive]
implements the specified traits. Clone
is not directly supported by #[enum_derive]
, so it passing to #[derive]
.
Code like this will be generated:
#
#
# extern crate auto_enumerate;
+ Clone
#
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.