知识卡片
生成类型的命名冲突
每次展开都生成名为 Builder 的类型时,同一模块中两个派生调用会争用名称。把输入名纳入输出,如 GleipnirBuilder,可以把不同输入的常见冲突分开,仍须防范用户已定义同名项的情况。内部名称可采用不常见的前缀进一步减少碰撞。面向调用者的辅助类型需要可预测的名称,并遵守 Rust 命名约定。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Testing a builder macro 工作区、生成库与宏入口 · 查看原书依据
As usual, we retrieve the name of the struct after parsing. In this case, we do that because we want to have a helper struct for temporarily storing field values. And as we will be injecting this builder in user code, we cannot simply name it “Builder” because the chance of the name clashing with the user’s code is too great.
参考来源
- 位置:《Write Powerful Rust Macros》第06章《Testing a builder macro》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-6.xhtml#p27)
- 结论依据:原文以同模块重复展开产生两个 Builder 为反例,联系原名派生、内部辅助名与暴露给调用者的可预测名称。
- 原始内容:As usual, we retrieve the name of the struct after parsing. In this case, we do that because we want to have a helper struct for temporarily storing field values. And as we will be injecting this builder in user code, we cannot simply name it “Builder” because the chance of the name clashing with the user’s code is too great.