I'm trying to create a macro that to use on a struct, in which you get another struct as an argument, then concatenate the structa fields. This the code of the macro
#[proc_macro_attribute]
pub fn cat(args: proc_macro::TokenStream, input: proc_macro::TokenStream) -> proc_macro:: TokenStream {
let mut base_struct = parse_macro_input!(input as ItemStruct);
let mut new_fields_struct = parse_macro_input!(args as ItemStruct);
if let syn::Fields::Named(ref mut base_fields) = base_struct.fields {
if let syn::Fields::Named(ref mut new_fields) = new_fields_struct.fields {
for field in new_fields.named.iter() {
base_fields.push(field.clone());
}
}
}
return quote! {
#base_struct
}.into();
}
Here's a basic example of what I want it to be used
struct new_fields{
field: usize,
}
#[cat(new_fields)]
struct base_struct{
base_field, usize,
}
However, it will just convert the word 'new_fields' into a TokenStream then fail to compile because it isn't a struct. Is there a way to 'pass' a struct to the macro as an argument without writing the code inside the the macro usage?