#enum #macros #derive #attribute

macro auto_enums_core

An internal crate to support auto_enums - do not use directly

41 releases

✓ Uses Rust 2018 edition

0.7.5 Jun 3, 2020
0.7.3 Apr 19, 2020
0.7.1 Nov 16, 2019
0.5.9 Jul 7, 2019
0.3.2 Dec 27, 2018

#24 in #attribute

Download history 1935/week @ 2020-03-16 1963/week @ 2020-03-23 1110/week @ 2020-03-30 1796/week @ 2020-04-06 1493/week @ 2020-04-13 1213/week @ 2020-04-20 1242/week @ 2020-04-27 1661/week @ 2020-05-04 1810/week @ 2020-05-11 2149/week @ 2020-05-18 1903/week @ 2020-05-25 1500/week @ 2020-06-01 1522/week @ 2020-06-08 1574/week @ 2020-06-15 1471/week @ 2020-06-22 1308/week @ 2020-06-29

7,173 downloads per month
Used in 9 crates (via auto_enums)

Apache-2.0 OR MIT

1.5K SLoC


crates-badge docs-badge license-badge rustc-badge

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.


Add this to your Cargo.toml:

auto_enums = "0.7"

The current auto_enums requires Rust 1.31 or later.


#[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_enums::auto_enum;

fn foo(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => 1..10,
        _ => vec![5, 10].into_iter(),

#[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:

fn foo(x: i32) -> impl Iterator<Item = i32> {
    enum __Enum1<__T1, __T2> {

    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.

use auto_enums::auto_enum;
fn foo(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => 1..10,
        _ => match x {
            1 => vec![5, 10].into_iter(),
            _ => 0..=x,

See 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 auto_enums::enum_derive;

// `#[enum_derive]` implements `Iterator`, and `#[derive]` implements `Clone`.
#[enum_derive(Iterator, Clone)]
enum Foo<A, B> {

[std|core] libraries

Some traits support is disabled by default. Note that some traits have aliases.

When using features that depend on unstable APIs, the "unstable" feature must be explicitly enabled


See also iter-enum crate.


See also futures-enum crate.


See also io-enum crate.


  • 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" and "unstable" crate features)
  • FnMut (requires "fn_traits" and "unstable" crate features)
  • FnOnce (requires "fn_traits" and "unstable" crate features)
  • Generator (requires "generator_trait" and "unstable" crate features)


  • AsRef (requires "convert" crate feature)
  • AsMut (requires "convert" crate feature)



External libraries

You can add support for external library by activating the each crate feature.

futures(v0.3) (requires "futures03" or "futures" crate feature)

See also futures-enum crate.

futures(v0.1) (requires "futures01" crate feature)

rayon (requires "rayon" crate feature)

serde (requires "serde" crate feature)

tokio(v0.2) (requires "tokio02" crate feature)

tokio(v0.1) (requires "tokio01" crate feature)

Related Projects

  • derive_utils: A procedural macro helper for easily writing derives macros for enums.
  • futures-enum: #[derive(Future, Stream, Sink, AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead)] for enums.
  • io-enum: #[derive(Read, Write, Seek, BufRead)] for enums.
  • iter-enum: #[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, Extend)] for enums.


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.


~17K SLoC