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

Builders that take &mut self should panic when used a second time #83

Open
dtolnay opened this Issue May 31, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@dtolnay
Copy link
Member

dtolnay commented May 31, 2017

let builder = Builder::new().a().b();
let _ = builder.build();
builder.c(); // panic
let _ = builder.build(); // panic

If you want to support this behavior, provide Clone for the builder.

let builder = Builder::new().a().b();
let _ = builder.clone().build();
let _ = builder.c().build();

In general, the user should not be allowed to rely on a builder being in any particular state after calling the terminal method (build or similar). If the ergonomics of doing so weren't awful, the terminal method would take self by value instead of &mut self.

@Kixunil

This comment has been minimized.

Copy link

Kixunil commented May 31, 2017

Why not just consume builder instead? Compile-time checking FTW.

@dtolnay

This comment has been minimized.

Copy link
Member Author

dtolnay commented May 31, 2017

Because then you can't write Builder::new().a().b().build().

@Kixunil

This comment has been minimized.

Copy link

Kixunil commented May 31, 2017

Ah, I see. In that case I'd say consume everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.