colin-kiegel / rust-derive-builder

derive builder implementation for rust structs
https://colin-kiegel.github.io/rust-derive-builder/
Apache License 2.0
1.28k stars 82 forks source link

Allowing `each` and `into` on a vec #209

Closed Freyskeyd closed 2 years ago

Freyskeyd commented 3 years ago

Hello,

I'm trying to have a nice api for one of my crates and I was wondering if I could be able to have a struct like this:


#[derive(derive_builder::Builder, Debug, Clone)]
#[builder(setter(into))]
struct SessionManagementPacketResult {
    #[builder(default = "SessionState::Opening")]
    session_state: SessionState,
    #[builder(setter(each = "packet", into))]
    packets: Vec<Packet>,
}

I want to be able to do session.packet(xx).packet(xx) where xx implement From<xx> for Packet (the into part) but it seems to not work, or the into isn't applied to packet.

Any idea of a workaround on that?

Thank's!

TedDriggs commented 3 years ago

I believe the issue there is that Extend has its own type coercion magic going on and stacking the two breaks inference. See this discussion from the initial PR to add each.

If you can think of a way to do it, I'd probably make the API be:

#[builder(setter(each(name = "packet", into), into))]

That would avoid breaking backwards compatibility, and if there are some tradeoffs needed to make it work, this would make the author's choice explicit.

TedDriggs commented 2 years ago

This is fixed by #234