decorators

Crates.iodecorators
lib.rsdecorators
version0.1.4
sourcesrc
created_at2021-06-24 10:13:01.979857
updated_at2022-05-22 08:55:59.656283
descriptionA macro for generating decorator methods
homepage
repository
max_upload_size
id414337
size8,472
jpkneefel (jpkneefel)

documentation

README

A Procedural Macro for Decorators

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).

Example

#[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});
Commit count: 0

cargo fmt