Framework for building derive proc macros over structures (struct
and enum
).
Handles:
- Getting expressions referencing fields
- Building patterns for enums
- Using the same logic for deriving over a enum or struct
- Error handling and generating compile_error
output
- Generics on trait and structure (including conflict rectification)
This crate extends (and re-exports) the excellent syn and quote
Evaluate do_thing
function on every field (expect those with the #[ignore]
attribute)
```rust use synhelpers::{ syn::{parsequote, DeriveInput, GenericParam, Ident, Stmt}, procmacro2::Span, quote, derivetrait, FieldMut, HasAttributes, Trait, TraitItem, TypeOfSelf, Constructable, };
let mytrait = Trait {
name: parsequote!(::mycrate::MyTrait),
genericparameters: None,
items: vec![TraitItem::newmethod(
Ident::new("methodone", Span::callsite()),
None,
TypeOfSelf::Reference,
Vec::default(),
None,
|mut item| {
item.mapconstructable(|mut constructable| {
Ok(constructable
.getfieldsmut()
.fieldsiteratormut()
.flatmap(|mut field| -> Option
let r#struct: DeriveInput = parse_quote! { struct X { a: String, b: i32 } };
let stream = derivetrait(r#struct, mytrait);
asserteq!( stream.tostring(), quote! { #[automaticallyderived] impl ::mycrate::MyTrait for X { fn methodone(&self) { let X { a: ref _0, b: ref _1 } = self; dothing(0); dothing(1); } } }.tostring() ) ```
Design work in progress