• Stars
    star
    303
  • Rank 137,655 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

auto_enums

crates.io docs.rs license rustc build status

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

This crate is a procedural macro implementation of the features discussions in rust-lang/rfcs#2414. This idea is also known as "Anonymous sum types".

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.8"

Compiler support: requires rustc 1.56+

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

#[auto_enum] can also parse nested arms/branches by using the #[nested] attribute.

use auto_enums::auto_enum;

#[auto_enum(Iterator)]
fn foo(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => 1..10,
        #[nested]
        _ => 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].

#[enum_derive] supports many of the standard library traits and some popular third-party libraries traits such as rayon, futures, tokio. See documentation for a complete list of supported traits.

If you want to use traits that are not supported by #[enum_derive], you can use another crate that provides derives macros, or you can define derives macros 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),
}

Optional features

  • std (enabled by default)
    • Enable to use std library's traits.
  • ops
    • Enable to use [std|core]::ops's Deref, DerefMut, Index, IndexMut, and RangeBounds traits.
  • convert
    • Enable to use [std|core]::convert's AsRef and AsMut traits.
  • fmt
    • Enable to use [std|core]::fmt's traits other than Debug, Display and Write.
  • transpose_methods
    • Enable to use transpose* methods.
  • futures03
  • futures01
  • rayon
    • Enable to use rayon traits.
  • serde
    • Enable to use serde traits.
  • tokio1
  • tokio03
  • tokio02
  • tokio01
  • generator_trait
    • Enable to use [std|core]::ops::Generator trait.
    • Note that this feature is unstable and may cause incompatible changes between patch versions.
  • fn_traits
    • Enable to use [std|core]::ops's Fn, FnMut, and FnOnce traits.
    • Note that this feature is unstable and may cause incompatible changes between patch versions.
  • trusted_len
    • Enable to use [std|core]::iter::TrustedLen trait.
    • Note that this feature is unstable and may cause incompatible changes between patch versions.

type_analysis feature

Analyze return type of function and let binding.

Note that this feature is still experimental.

Examples:

use auto_enums::auto_enum;

#[auto_enum] // there is no need to specify std library's traits
fn func1(x: i32) -> impl Iterator<Item = i32> {
    match x {
        0 => 1..10,
        _ => vec![5, 10].into_iter(),
    }
}

#[auto_enum]
fn func2(x: i32) {
    // Unlike `feature(impl_trait_in_bindings)`, this works on stable compilers.
    #[auto_enum]
    let iter: impl Iterator<Item = i32> = match x {
        0 => Some(0).into_iter(),
        _ => 0..x,
    };
}

Please be careful if you return another traits with the same name.

Related Projects

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

License

Licensed under either of Apache License, Version 2.0 or MIT license 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.

More Repositories

1

cargo-llvm-cov

Cargo subcommand to easily use LLVM source-based code coverage (-C instrument-coverage).
Rust
741
star
2

pin-project

A crate for safe and ergonomic pin-projection.
Rust
447
star
3

cargo-hack

Cargo subcommand to provide various options useful for testing and continuous integration.
Rust
379
star
4

install-action

GitHub Action for installing development tools (mainly from GitHub Releases).
Shell
256
star
5

futures-async-stream

Async stream for Rust and the futures crate.
Rust
167
star
6

upload-rust-binary-action

GitHub Action for building and uploading Rust binary to GitHub Releases.
Shell
163
star
7

pin-project-lite

A lightweight version of pin-project written with declarative macros.
Rust
135
star
8

portable-atomic

Portable atomic types including support for 128-bit atomics, atomic float, etc.
Rust
96
star
9

create-gh-release-action

GitHub Action for creating GitHub Releases based on changelog.
Shell
60
star
10

parse-changelog

Simple changelog parser, written in Rust.
Rust
46
star
11

replace-await

Migration tool for replacing await! macro with await syntax.
Rust
41
star
12

cargo-minimal-versions

Cargo subcommand for proper use of -Z minimal-versions and -Z direct-minimal-versions.
Rust
40
star
13

easy-ext

A lightweight attribute macro for easily writing extension trait pattern.
Rust
36
star
14

derive_utils

A procedural macro helper for easily writing custom derives for enums.
Rust
25
star
15

const_fn

A lightweight attribute for easy generation of const functions with conditional compilations.
Rust
23
star
16

atomic-memcpy

Byte-wise atomic memcpy.
Rust
21
star
17

setup-cross-toolchain-action

GitHub Action for setup toolchains for cross compilation and cross testing for Rust.
Shell
21
star
18

syn-serde

Library to serialize and deserialize Syn syntax trees.
Rust
15
star
19

futures-enum

#[derive(Future, Stream, Sink, AsyncRead, AsyncWrite, AsyncSeek, AsyncBufRead)] for enums.
Rust
13
star
20

atomic-maybe-uninit

Atomic operations on potentially uninitialized integers.
Rust
13
star
21

easytime

Providing wrapper types for safely performing panic-free checked arithmetic on instants and durations.
Rust
13
star
22

rust-cross-toolchain

Toolchains for cross compilation and cross testing for Rust.
Shell
12
star
23

cargo-no-dev-deps

Cargo subcommand for running cargo without dev-dependencies.
Rust
11
star
24

cargo-config2

Load and resolve Cargo configuration.
Rust
10
star
25

iter-enum

#[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, Extend)] for enums.
Rust
9
star
26

negative-impl

Negative trait implementations on stable Rust.
Shell
9
star
27

coverage-helper

Helper for https://github.com/taiki-e/cargo-llvm-cov/issues/123.
Shell
6
star
28

find-crate

Find the crate name from the current Cargo.toml.
Rust
5
star
29

syn-mid

Providing the features between "full" and "derive" of syn.
Rust
5
star
30

assert-unmoved

A type that asserts that the underlying type is not moved after being pinned and mutably accessed.
Rust
4
star
31

cache-cargo-install-action

GitHub Action for `cargo install` with cache.
Shell
4
star
32

io-enum

#[derive(Read, Write, Seek, BufRead)] for enums.
Shell
4
star
33

semihosting

Semihosting for AArch64, ARM, RISC-V, MIPS, and MIPS64
Rust
3
star
34

target-spec-json

Structured access to rustc --print target-spec-json and --print all-target-specs-json.
Rust
2
star
35

build-context

Make build environment/target information available as constants in normal libraries and binaries.
Shell
2
star
36

github-actions

Shell
2
star
37

iced_style_config

Create Iced style sheets from configuration files.
Rust
2
star
38

dependabot-config

Structured access to the Dependabot configuration file.
Rust
2
star
39

taiki-e

1
star
40

workflows

Shell
1
star
41

checkout-action

GitHub Action for checking out a repository. (Simplified actions/checkout alternative without depending on Node.js.)
Shell
1
star
42

test

Shell
1
star
43

dockerfiles

Shell
1
star