mozilla/rust — pull request #14755 by thomaslee (2014-06-08 20:53:33+00:00)
Title: TcpListener::bind/TcpStream::connect (&str, Int), &str and SocketAddr{...} overrides

cc @alexcrichton

In #13919 we changed `TcpStream::connect` to accept a host and a port parameter instead of a somewhat "uncomfortable" `SocketAddr`. There were some reasonable complaints following the merge that basically said sometimes you might *want* a SocketAddr for various different reasons, and that carting strings around everywhere might be painful in certain situations.

@huonw and @zargony suggested it might be better for `TcpStream::connect` and `TcpListener::bind` to use traits so that they can operate with various different types of arguments. I gave that a shot, and I think the results are really nice.

This change allows the following:

// (&str, Int)
let conn1 = try!(TcpStream::connect(("mozilla.org", 80))

// &str (I think this is what @alexcrichton originally wanted the API to look like prior to #13919)
let conn2 = try!(TcpStream::connect("mozilla.org:80")

// SocketAddr{...} (this is the old pre-#13919 API)
let conn3 = try!(TcpStream::connect(SocketAddr{ip: Ipv4Addr(63, 254, 215, 20), port: 80}))

It would also be trivial to add support for e.g. (IpAddr, Int) tuples too, but I didn't want to get too carried away :)

I think this PR needs some work before it gets merged. In particular, `connect_timeout` hasn't been modified, and I'm not sure that we want two distinct traits (`ToBindSocketAddr` and `ToSocketAddrs`) for very similar use-cases. Figure it's at a point where it'll probably drive some discussion, though.