我正在编写一个过程宏,它接受结构的字段并将它们发送到另一个方法:
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;
}
}
}当我尝试使用应该在self方法上接收的#vis async fn insert(&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` 发布于 2022-03-08 17:31:27
正如@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;
}
}
}https://stackoverflow.com/questions/71397717
复制相似问题