auto_enums
A library for to allow multiple return types by automatically generated enum.
This crate is a procedural macro implementation of the features discussions in https://github.com/rust-lang/rfcs/issues/2414.
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.
Usage
Add this to your Cargo.toml:
[]
= "0.6.0-alpha.1"
The current auto_enums requires Rust 1.31 or later.
Examples
#[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.
use auto_enum;
#[auto_enum] generates code in two stages.
First, #[auto_enum] will do the following.
- parses syntax
- creates the enum
- inserts variants
Code like this will be generated:
Next, #[enum_derive] implements the specified traits.
Code like this will be generated
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 (derive_utils probably can help it).
Basic usage of #[enum_derive]
use enum_derive;
// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.
[std|core] libraries
Note that some traits have aliases. Also, some traits support is disabled by default.
[std|core]::iter
Iterator- generated codeDoubleEndedIterator- generated codeExactSizeIterator- generated codeFusedIterator- generated codeTrustedLen- generated code (requires"trusted_len"crate feature) - nightly-onlyExtend- generated code
[std|core]::future
std::io
Read(alias:io::Read) - generated codeBufRead(alias:io::BufRead) - generated codeWrite(alias:io::Write) - generated codeSeek(alias:io::Seek) - generated code
[std|core]::ops
Deref(requires"ops"crate feature)DerefMut(requires"ops"crate feature)Index(requires"ops"crate feature)IndexMut(requires"ops"crate feature)RangeBounds(requires"ops"crate feature)Fn(requires"fn_traits"crate feature) - nightly-onlyFnMut(requires"fn_traits"crate feature) - nightly-onlyFnOnce(requires"fn_traits"crate feature) - nightly-onlyGenerator(requires"generator_trait"crate feature) - nightly-only
[std|core]::convert
[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::error
External libraries
You can add support for external library by activating the each crate feature.
futures(v0.3) (requires "futures" crate feature)
futures::Stream- generated codefutures::Sink- generated codefutures::AsyncRead- generated codefutures::AsyncWrite- generated codefutures::AsyncSeek- generated codefutures::AsyncBufRead- generated code
futures(v0.1) (requires "futures01" crate feature)
quote (requires "proc_macro" crate feature)
rayon (requires "rayon" crate feature)
rayon::ParallelIterator- generated coderayon::IndexedParallelIterator- generated coderayon::ParallelExtend- generated code
serde (requires "serde" crate feature)
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.