Create multiple items with a proc macro attached to an impl?


#1

With custom derive we can currently create multiple items from an attribute attached to a struct.

Is it possible to create new functions that are not inside the impl from a proc macro attached to an “impl X for Y”? (I appreciate that I can modify the contents of the impl to my hearts content). I’ve had a look into how custom derive does it (MultiItemModifier) but my understanding is that MultiItemModifier is being phased out in preference of something else.

  • Q1: Does the something else exist yet?
  • Q2: If I try and use MultiItemModifier on an impl is it likely to work or will it tell me that only custom derive is allowed to use it?

#2

My bad, I can output the existing Item and then join on other things using quote.
Here’s an example for others:

#[proc_macro_attribute]
pub fn add_item_example(_attr: TokenStream, input: TokenStream) -> TokenStream {
let mut results = quote::Tokens::new();
let mut ast: syn::Item = syn::parse(input.clone()).unwrap();
results.append_all(quote!(#ast));
results.append_all(quote!( #[test]fn testme(){}));
results.into()
}