自己在 proc_macro

self on a proc_macro

我正在编写一个过程宏,它获取结构的字段并将它们发送到另一个方法:

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;
        }
    }
}

当我尝试使用应该在 #vis async fn insert(&self) 方法上接收的 self 时,我收到错误消息:

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

如果我尝试将引用宏的签名上可用的 &self. 连接到 quote! {} 本身,我会收到此编译器错误:

   |
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` 

正如@Shepmaster 指出的那样,问题是我将列的名称字符串化,所以最终代码将如下所示:

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;
        }
    }
}