Request to implement ToSocketAddr trait on std::net::connect_timeout

i noticed this when changing my implementation from using connect to connect_timeout std::net::TcpStream::connect accepts string since it accepts ToSocketAddr trait as parameter. while connect_timeout doesn't do the same.

is there any plan for this in future?

I think for things like this you could probably just submit a PR behind a feature gate.

Unfortunately, adding trait parameters to functions is strictly speaking a breaking change, because the fully elaborated form of a function includes an empty turbofish, which would then stop working if a generic parameter is introduced. Adding a generic parameter would require default generic arguments for functions, which is a complicated bag of rabbit holes due to not quite actually being a normal default to work how people expect.

1 Like

connect accepts A: ToSocketAddrs, which might resolve to multiple SocketAddr. It then tries to connect to them in order, returning with the first successful one.

connect_timeout specifically calls out directly why it takes a single SocketAddr:

Unlike connect , connect_timeout takes a single SocketAddr since timeout must be applied to individual addresses.

That makes it predictable. Taking ToSocketAddrs could make it wait arbitrarily long, depending on how many addresses the endpoint resolves too. If that's the behavior you want it's at least easy enough to implement yourself.


As an aside, do you have links to issues about this? I’ve run into wanting defaults for type generic functions when making APIs a few times, and would be interested in knowing more about why it doesn’t work.

Thanks for your detailed explanation, So this means implementing this would not be possible in the future as well? shall we close the item then?

I'm not on the libs team and thus not responsible for this, but given that there is an explicit comment on why it does it the way it does I'd say it's very unlikely to change.

1 Like

The only description I know of is Interaction of user-defined and integral fallbacks with inference.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.