Skip to main content

batch_impl

Attribute Macro batch_impl 

Source
#[batch_impl]
Expand description

为 trait 批量生成 impl 块的属性宏。

在 trait 定义上标注 #[batch_impl(...)],宏参数中的每个 impl-spec 都会 为该 trait 生成一个对应的 impl 块。

§语法

#[batch_impl( impl-spec [, impl-spec]* [{ body }]? )]

impl-spec 由三部分组成(均可省略后半部分):

  • <impl-泛型>impl 块的泛型参数
  • Trait名<trait-泛型> — trait 的泛型参数与关联类型绑定
  • 目标类型 — 用 [] 包裹表示并列,用 ^/- 表示泛型应用

§示例

#[batch_impl(usize, isize)]
trait Numeric {}

#[batch_impl(<T> Vec<T>)]
trait Collection {}

#[batch_impl(<T> FromValue<T> [i32 { fn wrap(_: T) -> Self { 0 }}, u32 #wrap{0}] )]
trait FromValue<T> { fn wrap(val: T) -> Self; }

// #name{body} 也支持 const 和 type 项
#[batch_impl(usize #MY_CONST{42})]
trait HasConst { const MY_CONST: usize; }