All Projects → taiki-e → Auto_enums

taiki-e / Auto_enums

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

Programming Languages

rust
11053 projects

Labels

Projects that are alternatives of or similar to Auto enums

Cortex M Rt
Minimal startup / runtime for Cortex-M microcontrollers
Stars: ✭ 286 (+52.13%)
Mutual labels:  no-std
Heapless
Heapless, `static` friendly data structures
Stars: ✭ 575 (+205.85%)
Mutual labels:  no-std
Bitmatch
A Rust crate that allows you to match, bind, and pack the individual bits of integers.
Stars: ✭ 82 (-56.38%)
Mutual labels:  no-std
Embedded Graphics
A no_std graphics library for embedded applications
Stars: ✭ 293 (+55.85%)
Mutual labels:  no-std
Cortex M
Low level access to Cortex-M processors
Stars: ✭ 379 (+101.6%)
Mutual labels:  no-std
Xargo
The sysroot manager that lets you build and customize `std`
Stars: ✭ 841 (+347.34%)
Mutual labels:  no-std
littlefs2
Idiomatic Rust API for littlefs
Stars: ✭ 19 (-89.89%)
Mutual labels:  no-std
Utest
Unit `#[test]`ing for microcontrollers and other `no_std` systems
Stars: ✭ 119 (-36.7%)
Mutual labels:  no-std
Serde
Serialization framework for Rust
Stars: ✭ 4,901 (+2506.91%)
Mutual labels:  no-std
Wyhash Rs
wyhash fast portable non-cryptographic hashing algorithm and random number generator in Rust
Stars: ✭ 44 (-76.6%)
Mutual labels:  no-std
Nvptx
How to: Run Rust code on your NVIDIA GPU
Stars: ✭ 335 (+78.19%)
Mutual labels:  no-std
Cortex M Quickstart
Template to develop bare metal applications for Cortex-M microcontrollers
Stars: ✭ 372 (+97.87%)
Mutual labels:  no-std
Byte
A low-level, zero-copy, panic-free, binary serializer and deserializer. (parser and encoder)
Stars: ✭ 29 (-84.57%)
Mutual labels:  no-std
Rubble
(going to be a) BLE stack for embedded Rust
Stars: ✭ 292 (+55.32%)
Mutual labels:  no-std
Governor
A rate-limiting library for Rust (formerly ratelimit_meter)
Stars: ✭ 99 (-47.34%)
Mutual labels:  no-std
Drone Core
The core crate for Drone, an Embedded Operating System.
Stars: ✭ 263 (+39.89%)
Mutual labels:  no-std
Cortex M Rtic
Real-Time Interrupt-driven Concurrency (RTIC) framework for ARM Cortex-M microcontrollers
Stars: ✭ 623 (+231.38%)
Mutual labels:  no-std
Pin Project
A crate for safe and ergonomic pin-projection.
Stars: ✭ 174 (-7.45%)
Mutual labels:  no-std
Drone
CLI utility for Drone, an Embedded Operating System.
Stars: ✭ 114 (-39.36%)
Mutual labels:  no-std
Rhai
Rhai - An embedded scripting language for Rust.
Stars: ✭ 958 (+409.57%)
Mutual labels:  no-std

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

Compiler support: requires rustc 1.31+

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
  • rayon
    • Enable to use rayon traits.
  • serde
    • Enable to use serde traits.
  • tokio1
  • 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.
  • 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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].