Crates.io | withers_derive |
lib.rs | withers_derive |
version | 0.2.0 |
source | src |
created_at | 2018-04-02 11:02:52.494501 |
updated_at | 2018-04-02 15:32:55.217195 |
description | A macro to implement wither methods for properties of a struct. |
homepage | |
repository | https://github.com/Rowmance/withers-derive |
max_upload_size | |
id | 58601 |
size | 7,211 |
A macro to implement wither methods for properties of a struct.
The wither pattern consists of using methods to mutate an instance of a struct, generally beginning with with_
.
#[derive(Withers, Default)]
struct Foo {
bar: u32,
baz: Option<bool>
}
fn main() {
let instance = foo::default()
.with_bar(32)
.with_baz(Some(false));
}
This implementation generates consuming methods, so the instance is mutated and consumed, and can no longer be used in its previous state.
The wither pattern bares strong resemblance to the builder pattern, though:
clone
and/or copy
).However:
If these feel like they may be issues for you, the derive_builder crate may suit your needs better.
The following code
#[macro_use]
extern crate withers_derive;
#[derive(Withers)]
struct Foo {
bar: u32,
baz: Option<bool>
}
Will generate code akin to
struct Foo {
bar: u32,
baz: Option<bool>
}
#[allow(dead_code)]
impl Foo {
fn with_bar(mut self, value: u32) -> Self {
self.bar = value;
self
}
fn with_baz(mut self, value: Option<bool>) -> Self {
self.baz = value;
self
}
}
wither_derive
dependency to your Cargo.toml
file.#[macro_use]
extern crate withers_derive;
#[derive(Withers)]
.Please fork the repository and raise pull requests into master. Larger code changes should be tracked with GitHub issues.