#macro-derive #macro-helpers #enums #derive #macro

derive_utils

A procedural macro helper for easily writing derive macros for enums

29 releases

0.14.2 Aug 23, 2024
0.14.1 Jan 27, 2024
0.13.2 Jul 27, 2023
0.13.0 Mar 26, 2023
0.5.3 Dec 27, 2018

#20 in Procedural macros

Download history 54954/week @ 2024-07-30 53890/week @ 2024-08-06 59567/week @ 2024-08-13 56440/week @ 2024-08-20 63888/week @ 2024-08-27 68581/week @ 2024-09-03 59117/week @ 2024-09-10 57496/week @ 2024-09-17 59524/week @ 2024-09-24 60672/week @ 2024-10-01 56442/week @ 2024-10-08 74057/week @ 2024-10-15 76530/week @ 2024-10-22 78926/week @ 2024-10-29 77454/week @ 2024-11-05 53449/week @ 2024-11-12

300,380 downloads per month
Used in 358 crates (9 directly)

Apache-2.0 OR MIT

38KB
475 lines

derive_utils

crates.io docs.rs license msrv github actions

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

Usage

Add this to your Cargo.toml:

[dependencies]
derive_utils = "0.14"

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(),
        }
    }
}
  • auto_enums: A library for to allow multiple return types by automatically generated enum.
  • io-enum: #[derive(Read, Write, Seek, BufRead)] for enums.
  • iter-enum: #[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, 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

~245–700KB
~17K SLoC