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

NoK*_*Key 2 rust rust-proc-macros

我收到此错误:

该特征quote::to_tokens::ToTokens未实现 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()
}
Run Code Online (Sandbox Code Playgroud)

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

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

    // Expand to (a, b);
    between!(a, b);
}
Run Code Online (Sandbox Code Playgroud)

我还有一个包含上述代码的小项目: https: //bitbucket.org/JoshSinne/pm/src/master/。为什么我无法转换Ident内部令牌?我尝试使用parseinto但这对我来说没有成功。谢谢!

Cer*_*rus 6

syn是针对proc_macro2quote的替代实现构建的。如果您想使用它们,您应该首先将您的转换为,进行必要的操作,最后再转换回来。这两种转换都可以使用/进行,因为它们是双向实现的。proc_macroproc_macro::TokenStreamproc_macro2::TokenStreamFromInto