-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add Builder guidelines #1036
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
Add Builder guidelines #1036
Conversation
|
||
## Clone | ||
|
||
Builder objects should strive to implement the `Clone` and `Copy` traits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clone sure, but I'd remove Copy.
The guidelines may be a good place for this. |
This is guidelines for the standard library, not the overall ecosystem. This is also an RFC, because it has potential to make breaking changes to builders that already exist in the standard library. |
Could you list all the proposed changes to
|
Sure, I’ve been thinking I should do that, but I won’t be able to get to it until weekend, sadly. |
the builder object as `&mut self` rather than `self` unless there is a good reason to do otherwise. | ||
|
||
Finalizers – methods which produce the built object – should consume the builder object (i.e. | ||
receive `self`, rather than a reference), unless there is a good reason to do otherwise. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that this finalizer convention actually needs to be the same as the above convention which is to say that Foo::new().bar().finalize()
won't actually work if it consumes self
(as bar()
returned &mut self
), perhaps this could recommend "a borrowed self" instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, this is very disappointing, because moving would avoid copying owned data internally in most cases.
Thanks for the RFC @nagisa! I definitely agree that this is an area that looks like it could use some better guidelines. I've left some specific comments with things that @aturon and I talked about, but I wanted to talk more broadly about two specific aspects:
|
I think I’ve addressed most of the feedback:
|
3b1ded0
to
cc786b0
Compare
|
||
### `process::Command` | ||
|
||
`impl Clone for Command` is provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past I've been hesitant to do this as it may prevent us from adding more options to Command
in the future. For example if a standard I/O handle is redirected to a file, it's not clear how Clone
can be implemented.
We do have various routes to mitigate this, but this may fall under the category of "would like clone, but implementation may not make it possible".
I think this adds helpful consistency, so +1. I would also be in support of renaming |
A tentative 👍 here, but I'd rather keep As a separate argument for the name, |
I broadly like this RFC. For reasons that others have mentioned, I would prefer to keep the One thing I'm confused about is this in the "alternatives" section:
It seems to me like this could be a legitimate choice when designing builder-like things. I find that the reduction in the number of types to be a big advantage when possible. In particular, putting the builder on the buildee itself has worked well for me when there are a lot of optional parameters. e.g., let mut rdr = csv::Reader::from_file("foo.csv"); // sane defaults
let mut rdr = csv::Reader::from_file("foo.csv")
.delimiter('\t'); // tab delimited
let mut rdr = csv::Reader::from_file("foo.csv")
.has_headers(false)
.quote('\'')
.delimiter(';')
.escape(Some('\\')); // something whacky
// any of the above `rdr`s can decode records. It's true that this pattern can't be used for all things, but I'd find it unfortunate to rule it out just for that reason. |
@nagisa So, at this point it's pretty clear that the ball was dropped on this RFC (sorry!), and of course several of the changes it proposes would be breaking at this point. While I would still like to have some RFC'ed conventions around builders, I think at this point we need to declare bankruptcy on this RFC, take a look at what's happened in external crates (and things like net2), and start again with a fresh RFC. If you're up for that, great! If not, I understand, and the libs team will try to make something happen here. As per the libs team meeting, I'm going to close this RFC. Thanks for the proposal! |
No description provided.