Crates.io | decorators |
lib.rs | decorators |
version | 0.1.4 |
source | src |
created_at | 2021-06-24 10:13:01.979857 |
updated_at | 2022-05-22 08:55:59.656283 |
description | A macro for generating decorator methods |
homepage | |
repository | |
max_upload_size | |
id | 414337 |
size | 8,472 |
The Decorator macro generates a decorator method for each field, which is marked with #[dec]
in front.
Additionaly a field of type Option<T>
can be marked with #[opt_dec]
. This generates a decorator method that will set the value to Some(t)
.
#[derive(Decorator)]
struct Widget {
#[dec]
width: u32,
#[dec]
height: u32,
#[opt_dec]
background_color: Option<RGBA>,
}
Generates into:
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
}
}
}
Which can be used like:
let w = some_widget.width(10).height(20);
assert_eq!(w, Widget {width: 10, height: 20, background_color: None});