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 an option to extend collection-like fields #196
Conversation
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.
Thanks for putting this together, and great work on the code. Do we have examples of analogous builder APIs from crates that hand-implement their builders? The syntax for this feels a bit strange, but that might just be that it's new to me.
@TedDriggs I am not aware of such crates. I agree that name of setter
|
Your vocabulary is totally up to the task; the challenge here is that the underlying concept is more complex than a single word will accurately convey. In the original issue, one proposal is that for some field pub struct MyStruct {
#[builder(setter(multiple(replace = "policies")))]
policy: Vec<Policy>,
} This would then generate pub struct MyStructBuilder {
policy: Option<Vec<Policy>>,
}
pub struct MyStructBuilder {
policy: Option<Vec<Policy>>,
}
impl MyStructBuilder {
pub fn policy(&mut self, value: Policy) -> &mut Self {
self.policy
.get_or_insert_with(|| Default::default())
.extend(::std::iter::once(policy));
self
}
pub fn policies(&mut self, value: Vec<Policy>) -> &mut Self {
self.policy = Some(value);
self
}
} The second setter is only emitted if
Thoughts? |
@TedDriggs What if a field is named in plural form? In the case of What about |
In David Tolnay's proc-macro workshop, he uses the syntax: |
I like |
Closing in favor of #199 |
Addresses #114