Replies: 21 comments 10 replies
-
通俗易懂,感谢。 |
Beta Was this translation helpful? Give feedback.
-
过程宏的描述和例子太简单了,这里面还有很多内容,例如怎么去解析语法树。 |
Beta Was this translation helpful? Give feedback.
-
建议把最后的一段话放在最前面 |
Beta Was this translation helpful? Give feedback.
-
pub fn my_default(input: TokenStream) -> TokenStream {
...
} 里面那段代码完整吗? |
Beta Was this translation helpful? Give feedback.
-
示例代码有错 use proc_macro::TokenStream;
use syn::{self, Data};
use quote::{quote};
use syn::{parse_macro_input, DeriveInput};
#[proc_macro_derive(MyDefault)]
pub fn my_default(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let id = ast.ident;
if let Data::Struct(s) = ast.data {
// 声明一个新的ast,用于动态构建字段赋值的token
let mut field_ast = quote! {};
// 这里就是要动态添加token的地方了,需要动态完成Self的字段赋值
for (idx, f) in s.fields.iter().enumerate() {
let (field_id, field_ty) = (&f.ident, &f.ty);
if let Some(field_id) = field_id {
// 对于命名字段,都需要添加 `#field_name: #field_type::default(),` 这样的代码
field_ast.extend(quote! { #field_id: <#field_ty>::default(), });
} else {
let field_idx = syn::Index::from(idx);
let fty = &f.ty;
//对于匿名字段,都需要添加 `#field_idx: <#field_type as std::default::Default>::default(),` 这样的代码
field_ast.extend(quote! { #field_idx: <#fty>::default(), });
}
}
let result = quote! {
impl std::default::Default for #id {
fn default() -> Self {
Self { #field_ast }
}
}
}
.into();
result
} else {
panic!("MyDefault derive macro must use in struct");
}
} |
Beta Was this translation helpful? Give feedback.
-
($($x:expr),*) 这种方式并不能匹配结尾逗号 |
Beta Was this translation helpful? Give feedback.
-
少了点代码 #[proc_macro_derive(MyDefault)]
pub fn my_default(input: TokenStream) -> TokenStream {
let ast: DeriveInput = syn::parse(input).unwrap();
let id = ast.ident;
let Data::Struct(s) = ast.data else {
panic!("MyDefault derive macro must use in struct");
};
let mut field_ast = quote!();
for (idx, f) in s.fields.iter().enumerate() {
let (field_id, field_ty) = (&f.ident, &f.ty);
// 逗号不要忽略了
if field_id.is_none() {
let field_idx = syn::Index::from(idx);
field_ast.extend(quote! {#field_idx:<#field_ty>::default(),})
} else {
field_ast.extend(quote! {#field_id:<#field_ty>::default(),})
}
}
quote! {
impl Default for #id{
fn default() -> Self{
Self {#field_ast}
}
}
}
.into()
} |
Beta Was this translation helpful? Give feedback.
-
宏强大归强大,但是用起来ide没有提示也太草蛋了,跟用记事本写一样,clion和vscode都不太好使 |
Beta Was this translation helpful? Give feedback.
-
上面的宏无法匹配
只匹配一下的表达式
如果想匹配
那就表达式
|
Beta Was this translation helpful? Give feedback.
-
好了,成功地不认识“宏”字了 |
Beta Was this translation helpful? Give feedback.
-
《 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
好难,不求甚解过一遍都好难,以后如果用到了再回来翻可能会好点 |
Beta Was this translation helpful? Give feedback.
-
提点小建议: 模式解析 上面这段话有点小问题,按本文例子,3后面如果有括号是匹配不到的,会报错;只有3后面没有逗号才可以通过编译。 感谢此文,声明宏终于入门了一点点,功德无量。 |
Beta Was this translation helpful? Give feedback.
-
居然如此强大,岂不是可以利用宏的结构来自己定义一套语言了 |
Beta Was this translation helpful? Give feedback.
-
把宏设计成这样,真是个鬼才,设计的人不经常使用岂不是浪费了 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
error: missing parameters for function definition 依赖导入不了怎么解决,求大佬指点 |
Beta Was this translation helpful? Give feedback.
-
https://course.rs/advance/macro.html
Beta Was this translation helpful? Give feedback.
All reactions