知识卡片
特性应叠加功能
Cargo feature 可由不同依赖方请求。某个依赖方启用了原本未启用的 feature,同一库在统一解析后的能力应该增加。书中为两个宏各自设置可启用功能,分别启用时各增加一个入口。
如果某个 feature 的效果是移除原先就存在的 From 实现,新的依赖方开启它时会影响还在使用这个转换的调用方。全 crate 范围的 feature 宜提供正向能力,例如开启可选转换。某个结构体是否生成特定实现,也可以交给只影响该次展开的宏参数。
相关卡片 [[用特性控制宏的导出]]、[[按调用控制代码生成]]。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Macros and the outside world 用 feature 或参数控制功能 · 查看原书依据
NOTE In Rust, features should be additive, meaning they add functionality but never disable functionality that was already present in the crate. Cargo relies on this when resolving dependencies. Our macro obeys that rule, as its two features (struct and functional) add macros.
参考来源
- 位置:《Write Powerful Rust Macros》第10章《Macros and the outside world》(book-source:extracted/Write Powerful Rust Macros/_epub-src/OEBPS/Text/chapter-10.xhtml#p81)
- 结论依据:原书明确给出加法性规则,并以 feature 启用后关闭默认 From 输出为违例。
- 原始内容:NOTE In Rust, features should be additive, meaning they add functionality but never disable functionality that was already present in the crate. Cargo relies on this when resolving dependencies. Our macro obeys that rule, as its two features (struct and functional) add macros.