Search code examples
rustselfrust-proc-macros

self on a proc_macro


I am writing a procedural macro that takes the fields of a struct and sends them to another method:

pub fn my_helper_macro_builder(macro_data: &MacroTokens) -> TokenStream {
    // Retrieves the fields and tries to add self to take it's value
    let insert_values: Vec<TokenStream> = fields
        .iter()
        .map(|ident| ident.to_string())
        .collect::<Vec<String>>()
        .iter()
        .map(|column| quote! { self.#column })
        .collect();

    quote! {
        #vis async fn insert(&self) {
            <#ty as CrudOperations<#ty>>::__insert(
                #table_name,
                #column_names_pretty,
                &[
                    #(#insert_values),*
                ]
            ).await;
        }
    }
}

When I try to use the self that should be received on the #vis async fn insert(&self) method, I receive the error:

expected one of `,`, `.`, `;`, `?`, `]`, or an operator, found `"id"

If I try to wire the &self. available on the signature of the quoted macro into the quote! {} itself, I receive this compiler error:

   |
8  |   pub fn generate_insert_tokens(macro_data: &MacroTokens) -> TokenStream {
   |          ---------------------- this function can't have a `self` parameter
...
48 | /     quote! {
49 | |         #vis async fn insert(&self) {
50 | |             <#ty as CrudOperations<#ty>>::__insert(
51 | |                 #table_name, 
                       #column_names_pretty, 
                       &[
                            #self.#(#insert_values),*
                        ] 
...  |
57 | |         }
58 | |     }
   | |_____^ `self` value is a keyword only available in methods with a `self` parameter
   |
   = note: this error originates in the macro `$crate::quote_token_with_context` 

Solution

  • As @Shepmaster pointed, the issue it's that I was stringifying the name of the column, so the final code will look like this:

    pub fn my_helper_macro_builder(macro_data: &MacroTokens) -> TokenStream {
        // Retrieves the fields and tries to add self to take it's value
        let insert_values = fields.iter().map( |ident| {
            quote! { &self.#ident }
        });
    
        quote! {
            #vis async fn insert(&self) {
                <#ty as CrudOperations<#ty>>::__insert(
                    #table_name,
                    #column_names_pretty,
                    &[
                        #(#insert_values),*
                    ]
                ).await;
            }
        }
    }