知识卡片
同库复用多个过程宏
一个 proc-macro crate 能同时公开多种过程宏,并在内部使用普通类型和辅助函数。本章让函数式 config! 与属性宏 #[config_struct] 共用 ConfigInput 与 YAML 读取函数。函数式入口生成有映射字段的 Config,属性宏还解析被标注项,沿用它的名称和已有属性,把配置键生成具名字段,并产生构造方法。
书中例子重建结构体,原有字段会被替换,代码也没有通用保留泛型和可见性的逻辑。公开多个过程宏并不要求每个入口都维护一套相同的解析或读取逻辑。
相关卡片 [[编译期嵌入配置值]]、[[数据转字段需标识符]]、[[用特性控制宏的导出]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Macros and the outside world 同一 crate 的另一种宏入口 · 查看原书依据
Currently, we are exposing a nice function-like macro. But what if our users prefer to modify an existing struct with an attribute macro, instead of generating a new struct? Well, why not offer this option as well?
参考来源
- 位置:《Write Powerful Rust Macros》第10章《Macros and the outside world》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-10.xhtml#p51)
- 结论依据:原文说明过程宏库可公开多个宏,紧接着给出复用 ConfigInput 和不同输出生成器的属性宏。
- 原始内容:Currently, we are exposing a nice function-like macro. But what if our users prefer to modify an existing struct with an attribute macro, instead of generating a new struct? Well, why not offer this option as well?