Expand description
§Syn-Helpers
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 crates.
§Example
Evaluate do_thing
function on every field (expect those with the #[ignore]
attribute)
use syn_helpers::{
syn::{parse_quote, DeriveInput, GenericParam, Ident, Stmt}, proc_macro2::Span, quote,
derive_trait, FieldMut, HasAttributes, Trait, TraitItem, TypeOfSelf, Constructable,
};
let my_trait = Trait {
name: parse_quote!(::my_crate::MyTrait),
generic_parameters: None,
items: vec![TraitItem::new_method(
Ident::new("method_one", Span::call_site()),
None,
TypeOfSelf::Reference,
Vec::default(),
None,
|mut item| {
item.map_constructable(|mut constructable| {
Ok(constructable
.get_fields_mut()
.fields_iterator_mut()
.flat_map(|mut field| -> Option<Stmt> {
if field
.get_attributes()
.iter()
.any(|attr| attr.path().is_ident("ignore"))
{
None
} else {
let reference = field.get_reference();
Some(parse_quote!(do_thing(#reference);))
}
})
.collect())
})
},
)],
};
let r#struct: DeriveInput = parse_quote! {
struct X {
a: String,
b: i32
}
};
let stream = derive_trait(r#struct, my_trait);
assert_eq!(
stream.to_string(),
quote! {
#[automatically_derived]
impl ::my_crate::MyTrait for X {
fn method_one(&self) {
let X { a: ref _0, b: ref _1 } = self;
do_thing(_0);
do_thing(_1);
}
}
}.to_string()
)
§Used in
Design is a work in progress
Re-exports§
pub use proc_macro2;
pub use syn;
Macros§
- format_
ident - Formatting macro for constructing
Ident
s. - quote
- The whole point.
Structs§
- Argument
- A argument with a name and a value. ‘=’ separated, e.g.
name=AST
- Comma
Separated List - A comma separated list of
T
- Enum
Structure - An Enum
- Enum
Variant - A member of an EnumStructure
- Item
- A intermediate type that wraps a Structure
- Named
Field - Struct
Structure - A Struct
- Trait
- A declaration for a Rust trait
- Unnamed
Field
Enums§
- Constructable
Structure - Either a StructStructure or EnumVariant (with it’s parents attributes)
- Fields
- Represents the fields in a structure. This could be the fields on a struct or the fields in the variant of a enum
- Named
OrUnnamed Field - Either NamedField or UnnamedField
- Named
OrUnnamed Field Mut - Either NamedField or UnnamedField
- Structure
- Either a StructStructure or EnumStructure
- Trait
Item - A item under a trait
- Type
OfSelf - What ownership the method requires
Traits§
- Constructable
- A Rust structure which can be created (either a struct or enum variant)
- Field
- A Field is declaration of some data under a object
- Field
Mut - Getting a reference to the field is recorded
- HasAttributes
- ToTokens
- Types that can be interpolated inside a
quote!
invocation.
Functions§
- derive_
trait - Creates an impl block for the respective trait over the item
- derive_
trait_ from_ token_ stream - Same as derive_trait function, but handles parsing the TokenStream
- dyn_
error_ to_ compile_ error_ tokens - path_
to_ string - Prints a path out as its source representation