Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling some methods on the builder type directly without calling .build() #124

Open
KSXGitHub opened this issue Oct 16, 2023 · 1 comment

Comments

@KSXGitHub
Copy link

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

DoSomething::builder()
    .foo(foo)
    .bar(bar)
    .build() // <- I want to remove this
    .run()
    .await // the `into` attribute cannot handle opaque type such as `impl Future`, which was why calling `build` is the only way

What I hope for

DoSomething::builder()
    .foo(foo)
    .bar(bar)
    .run() // this function captures DoSomethingBuilder by move
    .await
@polarathene
Copy link

buildstructor might work for you?

  1. You impl a constructor (new() is fronted by generated builder()), with the supported fields that you want to set.
  2. Then during usage, when you're done call the 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.
  3. In this example we create an instance of self with those method params in 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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants