我得到这个错误:
特性
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()
}
这就是我想要使用它的方式:
fn main() {
let a = 1;
let b = 2;
// Expand to (a, b);
between!(a, b);
}
我还有一个小项目,它包含上面的代码:https://bitbucket.org/JoshSinne/pm/src/master/.为什么我不能在令牌中转换Ident
?我试过使用parse
、into
,但没有成功。谢谢!
syn
和quote
是针对proc_macro
的替代实现构建的,即proc_macro2。如果您想使用它们,您应该首先将proc_macro::TokenStream
转换为proc_macro2::TokenStream
,进行任何必要的操作,最后再转换回来。这两种转换都可以使用From
/Into
进行,因为它们是在两个方向上实现的。