A procedural macro helper for easily writing proc_macro_derive
like deriving trait to enum so long as all variants are implemented that trait.
Add this to your Cargo.toml
:
toml
[dependencies]
derive_utils = "0.4"
and this to your crate root:
rust
extern crate derive_utils;
```rust extern crate deriveutils; extern crate procmacro; extern crate proc_macro2; extern crate syn;
use deriveutils::{derivetrait, EnumData}; use procmacro::TokenStream; use procmacro2::{Ident, Span}; use syn::DeriveInput;
pub fn deriveiterator(input: TokenStream) -> TokenStream { let ast: DeriveInput = syn::parse(input).unwrap(); let data = EnumData::fromderive(&ast).unwrap();
derive_trait!(
data,
_,
// trait
trait Iterator {
type Item;
fn next(&mut self) -> Option<Self::Item>;
fn size_hint(&self) -> (usize, Option<usize>);
}
)
.unwrap()
.into()
}
pub fn deriveexactsizeiterator(input: TokenStream) -> TokenStream { let ast: DeriveInput = syn::parse(input).unwrap(); let data = EnumData::fromderive(&ast).unwrap();
derive_trait!(
data,
// super trait's associated types
Some(Ident::new("Item", Span::call_site())),
_,
// trait
trait ExactSizeIterator: Iterator {
fn len(&self) -> usize;
}
)
.unwrap()
.into()
}
pub fn derivefusediterator(input: TokenStream) -> TokenStream { let ast: DeriveInput = syn::parse(input).unwrap(); let data = EnumData::from_derive(&ast).unwrap();
derive_trait!(
data,
// super trait's associated types
Some(Ident::new("Item", Span::call_site())),
// path
(std::iter::FusedIterator),
// trait
trait FusedIterator: Iterator {}
)
.unwrap()
.into()
} ```
When deriving for enum like the following:
```rust
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
impl ExactSizeIterator for Iter
where
A: ExactSizeIterator,
B: ExactSizeIterator
impl std::iter::FusedIterator for Iter
where
A: std::iter::FusedIterator,
B: std::iter::FusedIterator
See auto_enums crate for more examples.
std
std
library.no_std
.The current minimum required Rust version is 1.30.
Licensed under either of
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.