知识卡片
按输入粒度选择宏参数
生成方法如果只依赖类型名,函数式过程宏可以仅接收 Ident 并输出该类型的 impl,结构体仍由调用者定义。生成字段级 accessor 则需要原字段名和类型。附录把这些信息从 DeriveInput 提取为可共享的列表,交给字段生成与 getter 生成两处逻辑。这样 pub fn get_...(&self) 返回字段类型的引用,同时重建的字段省去 pub。
相关卡片 [[按语法种类生成原项]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 5 章练习:按名字扩展与更换标点 · 查看原书依据
Now we use this information to generate our methods. The logic is almost the same as before, except the input is now a &Vec<(&Ident, &Type)> (a reference to the vector makes it easier to pass the field information to multiple methods).
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p70)
- 结论依据:第 5 章前两道题对比只读取结构体名与共享字段元数据,相关清单说明二者对应的生成范围。
- 原始内容:Now we use this information to generate our methods. The logic is almost the same as before, except the input is now a &Vec<(&Ident, &Type)> (a reference to the vector makes it easier to pass the field information to multiple methods).