#enum #macros #derive

derive_utils

A procedural macro helper for easily writing derive macros for enums

24 releases

0.12.0 Jan 10, 2022
0.11.2 Jan 5, 2021
0.11.1 Dec 29, 2020
0.11.0 Nov 6, 2020
0.5.3 Dec 27, 2018

#10 in Procedural macros

Download history 16642/week @ 2022-01-29 14145/week @ 2022-02-05 14615/week @ 2022-02-12 14401/week @ 2022-02-19 15372/week @ 2022-02-26 14564/week @ 2022-03-05 13633/week @ 2022-03-12 13524/week @ 2022-03-19 13661/week @ 2022-03-26 14908/week @ 2022-04-02 14652/week @ 2022-04-09 15226/week @ 2022-04-16 15233/week @ 2022-04-23 15180/week @ 2022-04-30 15473/week @ 2022-05-07 15019/week @ 2022-05-14

63,648 downloads per month
Used in 143 crates (7 directly)

Apache-2.0 OR MIT

28KB
420 lines

derive_utils

crates.io docs.rs license rustc build status

A procedural macro helper for easily writing derives macros for enums.

Usage

Add this to your Cargo.toml:

[dependencies]
derive_utils = "0.12"

Compiler support: requires rustc 1.31+

Examples

quick_derive! macro make easy to write proc_macro_derive like deriving trait to enum so long as all variants are implemented that trait.

use derive_utils::quick_derive;
use proc_macro::TokenStream;

#[proc_macro_derive(Iterator)]
pub fn derive_iterator(input: TokenStream) -> TokenStream {
    quick_derive! {
        input,
        // trait path
        std::iter::Iterator,
        // trait definition
        trait Iterator {
            type Item;
            fn next(&mut self) -> Option<Self::Item>;
            fn size_hint(&self) -> (usize, Option<usize>);
        }
    }
}

#[proc_macro_derive(ExactSizeIterator)]
pub fn derive_exact_size_iterator(input: TokenStream) -> TokenStream {
    quick_derive! {
        input,
        // trait path
        std::iter::ExactSizeIterator,
        // super trait's associated types
        <Item>,
        // trait definition
        trait ExactSizeIterator: Iterator {
            fn len(&self) -> usize;
        }
    }
}

Generated code

When deriving for enum like the following:

#[derive(Iterator, ExactSizeIterator, Future)]
enum Enum<A, B> {
    A(A),
    B(B),
}

Code like this will be generated:

enum Enum<A, B> {
    A(A),
    B(B),
}

impl<A, B> std::iter::Iterator for Enum<A, B>
where
    A: std::iter::Iterator,
    B: std::iter::Iterator<Item = <A as std::iter::Iterator>::Item>,
{
    type Item = <A as std::iter::Iterator>::Item;
    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Enum::A(x) => x.next(),
            Enum::B(x) => x.next(),
        }
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        match self {
            Enum::A(x) => x.size_hint(),
            Enum::B(x) => x.size_hint(),
        }
    }
}

impl<A, B> std::iter::ExactSizeIterator for Enum<A, B>
where
    A: std::iter::ExactSizeIterator,
    B: std::iter::ExactSizeIterator<Item = <A as Iterator>::Item>,
{
    fn len(&self) -> usize {
        match self {
            Enum::A(x) => x.len(),
            Enum::B(x) => x.len(),
        }
    }
}

Related Projects

  • auto_enums: A library for to allow multiple return types by automatically generated enum.
  • 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.

Dependencies

~250–670KB
~16K SLoC