rust-derive-builder icon indicating copy to clipboard operation
rust-derive-builder copied to clipboard

Using a collection setter with mismatched collections of Vec -> HashMap causes incorrect call to be made

Open TapGhoul opened this issue 2 years ago • 1 comments

When trying to create a builder that takes a Vec as the builder and builds into a HashMap, incorrect code seems to be generated if you try to generate a collection setter.

What happens is that add_buggy expects get_or_insert_with to exist on the Vec that is created for the builder, but it does not, so it fails.

A minimal reproduction case:

#[derive(Builder)]
struct MyBuggyBuilder {
    #[builder(
        field(
            type = "Vec<i32>",
            build = "self.buggy.into_iter().map(|e| (e,e)).collect()"
        ),
        setter(each(name = "add_buggy", into))
    )]
    pub buggy: HashMap<i32, i32>,
}

TapGhoul avatar Apr 10 '23 22:04 TapGhoul

The issue appears to be coming from these lines. It looks like this initialization code will need to take the field type into account.

I'm happy to review a PR for this, but implementing it myself isn't something I have time for.

TedDriggs avatar Apr 19 '23 13:04 TedDriggs