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_
.
```rust
struct Foo {
bar: u32,
baz: Option
fn main() { let instance = foo::default() .withbar(32) .withbaz(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: - The wither pattern requires a struct to be able to be initialised with default and sensible values. Otherwise an error which could be caught on initialisation could cause unexpected behaviour down the line. - A builder can usually be stored and used to generate several instances of structs, whereas withers operate on instances directly.
If these feel like they may be issues for you, thederive_buildercrate may suit your needs better.
The following code ```rust
extern crate withers_derive;
struct Foo {
bar: u32,
baz: Option
Will generate code akin to
rust
struct Foo {
bar: u32,
baz: Option
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
}
} ```
Please fork the repository and raise pull requests into master. Larger code changes should be tracked with GitHub issues.