Rust macro based on custom_derive to automatically implement the builder pattern for arbitrary structs. This is achieved by implementing setter-methods for all struct fields automatically.
This is a work in progress. Use it at your own risk.
And this is how it works:
```rust
customderive! { #[derive(Debug, Default, Builder)] struct Channel { token: i32, specialinfo: i32 } }
fn main() { let ch = Channel::default().special_info(42); println!("{:?}", ch); } ```
Note that we did not write any implementation of Channel
or method called special_info
. Instead the custom_derive!
macro scans the #[derive(..)]
attribute of the struct for non-std identifiers – in our case #[derive(Builder)]
– and delegates the code generation to the Builder!
macro defined in this crate.
The automatically generated setter method for the token
field will look like this:
rust
fn token<VALUE: Into<i32>>(mut self, value: VALUE) -> Self {
self.token = value.into();
self
}
self
.Into
trait for the field type.VALUE
, because this is already reserved for the setter-methods.VALUE
as a generic parameter as this is what all setters are using.Builder!
, make sure you don't use this name for another macro.The builder pattern is explained here. But please note that there is a debate about which variant is preferable for what reasons.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.