3个版本
| 0.1.2 | 2020年9月22日 |
|---|---|
| 0.1.1 | 2020年9月22日 |
| 0.1.0 | 2020年9月22日 |
#5 in #properly
8KB
92 行
qualify-derive
用于包装未正确指定路径的 derive 宏的简单实用工具。
何时需要?
例如,specs v0.16.1 有一个 derive 宏 Component。如果没有传递 #[storage] 到宏中,它将生成一行 type Storage = DenseVecStorage;,当 DenseVecStorage 未导入时会导致编译错误。
手动添加 derive 宏的导入非常麻烦。qualify-derive 可以帮助你创建一个包装宏,它会自动导入它们。
如何使用
使用此 Cargo.toml 创建一个 新的 包(当前包中无法使用过程宏)
[package]
name = # the usual [package] stuff
[lib]
proc-macro = true
[dependencies]
qualify-derive = "0.1.1"
然后创建 src/lib.rs 并包含以下内容
qualify_derive::declare! {
your_attribute_name derives ::full_path_to::TargetDeriveMacro;
use the_paths::you_want_to::import_automatically;
use import_groups_are::not_supported;
// use foo::{bar, qux}; // this does not work
attr this_line_is_optional
}
这声明了一个名为 your_attribute_name 的过程宏属性。在下游包中,您可以使用 #[your_attribute_name] 来替换 #[derive(::full_path_to::TargetDeriveMacro)]。
attr this_line_is_optional 将允许下游的 crate 使用 #[your_attribute_name(some content here)],它将扩展为 #[derive(::full_path_to::TargetDeriveMacro)] #[this_line_is_optional(some content here)]。
示例
请参阅 test-macro 和 test-lib。
限制
- 已知使用此 crate 生成的属性的类型在未使用时会有与
cargo fix相关的问题。当类型未使用时,cargo fix会 移除属性。 - 生成的属性将用匿名模块包裹用户类型。
super将引用类型本身声明的模块。然而,super范围内的所有符号都会自动导入到内部模块的作用域,因此不需要在类型内部使用super::;但任何对super的使用都是不正确的。请使用super::super或从crate::的绝对路径。
依赖项
~1.5MB
~35K SLoC