#enum #macros #derive #attribute

auto_enums

A library for to allow multiple return types by automatically generated enum

24 releases (4 breaking)

✓ Uses Rust 2018 edition

0.5.7 May 12, 2019
0.5.5 Mar 29, 2019
0.3.2 Dec 27, 2018

#41 in Rust patterns

Download history 33/week @ 2019-01-27 29/week @ 2019-02-03 8/week @ 2019-02-10 22/week @ 2019-02-17 35/week @ 2019-02-24 54/week @ 2019-03-03 71/week @ 2019-03-10 67/week @ 2019-03-17 105/week @ 2019-03-24 34/week @ 2019-03-31 11/week @ 2019-04-07 110/week @ 2019-04-14 111/week @ 2019-04-21 82/week @ 2019-04-28 112/week @ 2019-05-05

274 downloads per month
Used in 5 crates (1 directly)

Apache-2.0/MIT

51KB
288 lines

auto_enums

Build Status version documentation license Rustc Version

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:

[dependencies]
auto_enums = "0.5"

The current version of 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_enums::auto_enum;

#[auto_enum(Iterator)]
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> {
    #[::auto_enums::enum_derive(Iterator)]
    enum __Enum1<__T1, __T2> {
        __T1(__T1),
        __T2(__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

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

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

[std|core] libraries

Note that some traits have aliases.

[std|core]::ops

[std|core]::convert

[std|core]::iter

[std|core]::fmt

[std|core]::future

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)

License

Licensed under either of

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.

Dependencies

~1MB
~19K SLoC