Open KSXGitHub opened 1 year ago
buildstructor might work for you?
new()
is fronted by generated builder()
), with the supported fields that you want to set.build()
(the "exit" method, which has been renamed to run
) to indicate that you're done supplying mandatory params and that will then call the annotated new()
method with those values.new()
and call the structs .run()
method on it which we can do since the builders own .run()
is from the generated builder struct thus no conflict 👍 struct MyStruct {
foo: usize,
bar: usize,
}
#[buildstructor::buildstructor]
impl MyStruct {
#[builder(exit = "run")]
async fn new(foo: usize, bar: usize) -> bool {
(Self { foo, bar }).run()
}
fn run(self) -> bool {
self.foo == self.bar
}
}
#[tokio::main]
async fn main() {
let result = MyStruct::builder().foo(4).bar(2).run().await;
assert_eq!(result, false);
}
I use the builder pattern mainly to emulate named arguments. But I don't want to have to call
build
.What I must do currently
What I hope for