Hiya, have just published proc_macro_roids
, a crate that provide traits and functions to make writing proc macros more ergonomic.
Links:
Motivation:
Imagine a world where proc macros were easy.
Well, hopefully you don't have to imagine any longer!
Example:
-
Append derives.
#[proc_macro_attribute] pub fn copy(_args: TokenStream, item: TokenStream) -> TokenStream { // Example input: // // #[copy] // #[derive(Debug)] // struct Struct; let mut ast = parse_macro_input!(item as DeriveInput); let derives = parse_quote!(Clone, Copy); ast.append_derives(derives); // Example output: // // #[derive(Debug, Clone, Copy)] // struct Struct; TokenStream::from(quote! { #ast }) }
-
Append fields.
#[proc_macro_attribute] pub fn append_fields(_args: TokenStream, item: TokenStream) -> TokenStream { // Example input: // #[append_fields] // struct StructNamed { a: u32, b: i32 } let mut ast = parse_macro_input!(item as DeriveInput); let fields_additional: FieldsNamed = parse_quote!({ c: i64, d: usize }); ast.append(fields_additional); // Example output: // struct StructNamed { a: u32, b: i32, c: i64, d: usize } TokenStream::from(quote! { #ast }) }
More examples are in the README.md
and docs.