-
Notifications
You must be signed in to change notification settings - Fork 16
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
option to completely customise input type of builder methods #22
Comments
Thank you for bringing this up! This is something I was also thinking about. For example I didn't do this initially to keep the initial release simple and to keep the number of attributes and magic as low as possible. For example, today you can implement this by defining your builder via a use bon::bon;
struct ExampleStruct {
example: HashSet<u32>,
}
#[bon]
impl ExampleStruct {
#[builder]
fn new(example: impl IntoIterator<Item = u32>) -> Self {
Self { example: <_>::from_iter(example) }
}
} I understand that this requires a bit more boilerplate, however, this makes the code simpler. The fallback to a On the other hand, I suppose you'd like to have a general capability to accept I'd prefer a What do you think? Will |
Yeah, for me at least |
I agree. It's unfortunate this naming choice was established in Rust |
Yeah it's kind of weird but if that's the norm in rust then it definitely makes sense! |
Glad to hear that! I noticed that you added |
Oh yeah, that was from when you had fixed the issue about optional types having to implement default on main, but before you had published a new crates.io release. I have changed it just not committed those changes to gh yet! |
I'd love to be able to completely customise the input type for builder methods, for situations where
impl Into
isn't enough.For example, if I have a
HashSet<T>
as a struct field, it would be much nicer to accept animpl IntoIterator<Item = T>
then have the buider method doinput.into_iter().collect()
rather than the builder method acceptingimpl Into<HashSet<T>>
and the user having to do the rest.Not sure how this could be implemented, there would have to be a way to specify the desired input type, and also perhaps pass in to the macro a function that goes from the input type to the type of the struct field. E.g:
Or perhaps it could be possible to supply a closure:
A note for the community from the maintainers
Please vote on this issue by adding a 👍 reaction to help the maintainers with prioritizing it. You may add a comment describing your real use case related to this issue for us to better understand the problem domain.
The text was updated successfully, but these errors were encountered: