14个版本 (2个稳定版)
1.0.2 | 2023年12月22日 |
---|---|
1.0.0 | 2023年10月8日 |
0.4.2 | 2023年8月28日 |
0.4.1 | 2023年5月14日 |
0.1.2 | 2023年4月3日 |
#24 in 过程宏
每月294,672次下载
在418个crate中使用了它(通过 frame-support-procedural)
17KB
245 行
Proc Macro Warning
在proc宏内部发出警告。
Rust没有内置的函数从proc宏内部产生警告。这个crate为你的proc宏用例提供"已废弃"警告。
示例
使用构建器模式构建警告非常简单。
use proc_macro_warning::Warning;
let warning = Warning::new_deprecated("OldStuffUsed")
.old("my_macro()")
.new("my_macro::new()")
.help_link("https:://example.com")
.span(proc_macro2::Span::call_site())
.build_or_panic();
// Use the warning in a proc macro
let tokens = quote::quote!(#warning);
这适用于 derive-macros,但你必须设置一个span;否则它不会出现在编译输出中。
与#[deprecated]
属性的差异在于它会以任何方式发出警告。例如,当创建自定义的Deprecated
derive宏时,即使没有构造结构体也会发出警告。
#[derive(derive::Deprecated)]
struct Test {}
fn main() {
// Warning triggers although we never used `Test`.
// Otherwise use a normal `#[deprecated]`.
}
无意见格式化
创建警告的正常上述方式将强加特定的统一语法和格式化规则。
你可以选择退出并使用自己的方式,通过使用FormattedWarning::new_deprecated
use proc_macro_warning::FormattedWarning;
let warning = FormattedWarning::new_deprecated(
"my_macro",
"looooooooooooooooooooooooooooooong line that will not be brokeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen ;)",
proc_macro2::Span::call_site(),
);
// Use the warning in a proc macro
let tokens = quote::quote!(#warning);
一个类似示例的输出在 derive/src/lib.rs,在 derive_raw.stderr。
使用情况
Substrate使用它来为其eDSL(FRAME)的已废弃行为发出警告。集成是在 #13798 中完成的,展示了如何在宏扩展中使用这些警告。
警告格式统一,语法一致
warning: use of deprecated constant `pallet::warnings::ImplicitCallIndex_0::_w`:
It is deprecated to use implicit call indices.
Please instead ensure that all calls have the `pallet::call_index` attribute or that the `dev-mode` of the pallet is enabled.
For more info see:
<https://github.com/paritytech/substrate/pull/12891>
<https://github.com/paritytech/substrate/pull/11381>
--> frame/nomination-pools/src/lib.rs:2621:10
|
2621 | pub fn claim_commission(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
| ^^^^^^^^^^^^^^^^
|
另一个
warning: use of deprecated constant `pallet::warnings::ConstantWeight_0::_w`:
It is deprecated to use hard-coded constant as call weight.
Please instead benchmark all calls or put the pallet into `dev` mode.
For more info see:
<https://github.com/paritytech/substrate/pull/13798>
--> frame/nomination-pools/src/lib.rs:2620:20
|
2620 | #[pallet::weight(0)]
|
许可证
您可以选择以下任一许可证
- GNU通用公共许可证,版本3 (LICENSE-GPL3 或 gnu.org)
- Apache许可证,版本2.0 (LICENSE-APACHE2 或 apache.org)。
贡献
除非您明确说明,否则根据Apache-2.0许可证定义的,您有意提交以包含在工作中的任何贡献,应双许可如上所述,无需任何额外的条款或条件。
依赖关系
~285–740KB
~18K SLoC