引号内转换类型!给出特征错误

Converting type inside quote! gives trait errors

我收到此错误:

the trait quote::to_tokens::ToTokens is not implemented for proc_macro::Ident

当我尝试 运行 此代码时:

#[proc_macro_hack]
pub fn between(input: TokenStream) -> TokenStream {
    let idents = input
        .into_iter()
        .map(|i| match i {
            TokenTree::Ident(b) => {
                b
            },
            _ => panic!()
        })
        .collect::<Vec<_>>();

    let new_token_stream = quote! {
        (#(#idents),*)
    };

    new_token_stream.into()
}

这就是我想要的使用方式:

fn main() {
    let a = 1;
    let b = 2;

    // Expand to (a, b);
    between!(a, b);
}

我还有一个包含以上代码的小项目:https://bitbucket.org/JoshSinne/pm/src/master/。为什么我不能转换一个 Ident inside tokens?我尝试使用 parseinto,但这对我没有用。谢谢!

synquote 是针对 proc_macro 的替代实现构建的,即 proc_macro2. If you want to use them, you should first convert your proc_macro::TokenStream into proc_macro2::TokenStream,进行任何必要的操作,并在最后转换回来。这两种转换都可以使用 From/Into 进行,因为它们是双向实现的。