知识卡片
转发规则共享宏实现
同一个声明宏可以用简单的匹配分支补默认参数,再委派给一般分支。附录的 generate_from! 在只收到结构体名字时,补上 String 并再次调用自己,最终由接收类型参数的分支输出 From。
另一个组合宏可以把求底层值的方法和 From 方法绑定成一次调用。两个底层宏仍可独立使用。相同输出逻辑集中在一处,调用方可以选择方便的简写或显式指定类型的新类型转换。
相关卡片 [[尾随标点独立可选]]、[[声明宏生成过程宏样板]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Exercise solutions 第 2 章练习:匹配与组合 · 查看原书依据
Alternatively, you could add another rule for matching a trailing comma. In that case, you probably want to define the transcriber body only once and make one of the rules for multiple elements use the other one internally.
参考来源
- 位置:《Write Powerful Rust Macros》附录 A《Exercise solutions》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/appendix-a.xhtml#p11)
- 结论依据:原文建议由一个宏匹配分支调用另一个共享转写实现。紧接的 newtype 清单将该原则用于补默认类型及组合两个已有宏。
- 原始内容:Alternatively, you could add another rule for matching a trailing comma. In that case, you probably want to define the transcriber body only once and make one of the rules for multiple elements use the other one internally.