我正在尝试构建一个简单的属性,它将在函数的开头注入一个 let 绑定,因此结果将是:
#[foo]
fn bar(){
// start injected code
let x = 0;
// end injected code
...
}
我已经做到了:
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, ItemFn, Item, Stmt};
#[proc_macro_attribute]
pub fn hello(attr: TokenStream, stream: TokenStream) -> TokenStream {
let input = parse_macro_input!(stream as ItemFn);
let block = input.block;
let res = quote! {let x = 1;};
// doesn't work, can't convert TokenStream into Stmt
block.stmts.insert(0, res.into());
TokenStream::from(input.into_token_stream())
}
但是,我在处理这些项目时遇到了麻烦。例如,block
is 类型Vec<Stmt>
,现在一个语句由片段 ( Local
, Item
Expr
, Semi
) 组成,当尝试处理这些片段时,我只是迷路了。我觉得我遗漏了一些关于如何处理这些部分的内容,但是查看提供的示例trace-vars
并没有帮助,而且许多在线资源都已过时。
我还尝试了一种非常愚蠢的方法来创建ItemFn
using 引用,解析它并Stmt
从中获取,但是由于TokenStream
实际上来自两个不同的 crate,我得到了另一个错误,proc_macro
并且proc_macro2
:
#[proc_macro_attribute]
pub fn hello(attr: TokenStream, stream: TokenStream) -> TokenStream {
let input = parse_macro_input!(stream as ItemFn);
let res = quote! {fn () { let x = 1; } };
let res = parse_macro_input!(res as ItemFn);
let block = input.block;
block.stmts.insert(0, res.block.stmts[0].clone());
TokenStream::from(input.into_token_stream())
}
error[E0308]: mismatched types
--> bsub-procedural/src/lib.rs:13:15
|
13 | let res = parse_macro_input!(res as ItemFn);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `proc_macro::TokenStream`, found struct `proc_macro2::TokenStream`
继续运行
[dependencies]
syn = { version = "1.0.67", features=["full", "fold", "printing"] }
quote = "1.0.9"