derive_utils

Build Status version documentation license Rustc Version

A procedural macro helper for easily writing [custom derives] for enums.

Usage

Add this to your Cargo.toml:

toml [dependencies] derive_utils = "0.5"

and this to your crate root:

rust extern crate derive_utils;

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.

```rust extern crate deriveutils; extern crate procmacro;

use deriveutils::quickderive; use proc_macro::TokenStream;

[procmacroderive(Iterator)]

pub fn deriveiterator(input: TokenStream) -> TokenStream { quickderive! { input, // trait trait Iterator { type Item; fn next(&mut self) -> Option; fn size_hint(&self) -> (usize, Option); } } }

[procmacroderive(ExactSizeIterator)]

pub fn deriveexactsizeiterator(input: TokenStream) -> TokenStream { quickderive! { input, // super trait's associated types Item, // trait trait ExactSizeIterator: Iterator { fn len(&self) -> usize; } } }

[procmacroderive(FusedIterator)]

pub fn derivefusediterator(input: TokenStream) -> TokenStream { quick_derive! { input, // super trait's associated types Item, // path (std::iter::FusedIterator), // trait trait FusedIterator: Iterator {} } } ```

Generated code

When deriving for enum like the following:

```rust

[derive(Iterator, ExactSizeIterator, FusedIterator)]

enum Iter { A(A), B(B), } ```

Code like this will be generated:

```rust enum Iter { A(A), B(B), }

impl Iterator for Iter where A: Iterator, B: Iterator::Item>, { type Item = ::Item; fn next(&mut self) -> Option { match self { Iter::A(x) => x.next(), Iter::B(x) => x.next(), } } fn sizehint(&self) -> (usize, Option) { match self { Iter::A(x) => x.sizehint(), Iter::B(x) => x.size_hint(), } } }

impl ExactSizeIterator for Iter where A: ExactSizeIterator, B: ExactSizeIterator::Item>, { fn len(&self) -> usize { match self { Iter::A(x) => x.len(), Iter::B(x) => x.len(), } } }

impl std::iter::FusedIterator for Iter where A: std::iter::FusedIterator, B: std::iter::FusedIterator::Item>, { } ```

See auto_enums crate for more examples.

Crate Features

Rust Version

The current minimum required Rust version is 1.30.

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.