2个版本
| 0.1.4 | 2022年5月22日 | 
|---|---|
| 0.1.3 |  | 
| 0.1.2 | 2021年6月24日 | 
| 0.1.1 |  | 
| 0.1.0 |  | 
#16 in #decorator
8KB
78 行
装饰器进程宏
装饰器宏为每个带有前缀 #[dec] 的字段生成一个装饰器方法。另外,类型为 Option<T> 的字段可以用 #[opt_dec] 标记。这将生成一个将值设置为 Some(t) 的装饰器方法。
示例
#[derive(Decorator)]
struct Widget {
    #[dec]
    width: u32,
    #[dec]
    height: u32,
    #[opt_dec]
    background_color: Option<RGBA>,
}
生成到
struct Widget {
    width: u32,
    height: u32,
    background_color: Option<RGBA>,
}
impl Widget {
    pub fn width(self, width: u32) -> Self {
        Self {
            width,
            ..self
        }
    }
    pub fn height(self, height: u32) -> Self {
        Self {
            height,
            ..self
        }
    }
    pub fn background_color(self, background_color: RGBA) -> Self {
        Self {
            background_color: Some(background_color),
            ..self
        }
    }
}
可以像这样使用
let w = some_widget.width(10).height(20);
assert_eq!(w, Widget {width: 10, height: 20, background_color: None});
依赖项
~1.5MB
~36K SLoC