Rust struct builder implementation macro
Motivation
A derive macros to support a builder pattern for Rust:
- Everything except
Option<>fields and explicitly defineddefaultattribute in structs are required, so you don't need any additional attributes to indicate it, and the presence of required params is checked at the compile time (not at the runtime). - To create new struct instances there is
::newand an auxiliary init struct definition with only required fields (to compensate the Rust's named params inability).
Usage:
// Import it
use Builder;
// And use it on your structs
let s1 : MyStructure =
from
.with_opt_field1
.with_opt_field2;
The macros generates the following functions and instances for your structures:
with/without_<field_name>: immutable setters for fields<field_name>/reset_<field_name>: mutable setters for fieldsnew: factory method with required fields as argumentsFrom<>instance from an an auxiliary init struct definition with only required fields. The init structure generated as<YourStructureName>Init. So, you can usefrom(...)orinto()functions from it.
Defaults
use Builder;
Details and source code: [https://github.com/abdolence/rust-struct-builder]: https://github.com/abdolence/rust-struct-builder