Skip to content

option to completely customise input type of builder methods #22

@ThomasAlban

Description

@ThomasAlban

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 an impl IntoIterator<Item = T> then have the buider method do input.into_iter().collect() rather than the builder method accepting impl 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:

#[builder]
struct ExampleStruct {
    #[builder(input_type = impl IntoIterator<Item = u32>, convert_fn = convert)]
    example: HashSet<u32>,
}

fn convert(input: impl IntoIterator<Item = u32>) -> HashSet<u32> {
    input.into_iter().collect()
}

Or perhaps it could be possible to supply a closure:

#[builder]
struct ExampleStruct {
    #[builder(input_type = impl IntoIterator<Item = u32>, convert_fn = |x| x.into_iter().collect())]
    example: HashSet<u32>,
}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions