rust/src/libstd
Tom Lee a57889a580 Easier interface for TCP ::connect and ::bind.
Prior to this commit, TcpStream::connect and TcpListener::bind took a
single SocketAddr argument. This worked well enough, but the API felt a
little too "low level" for most simple use cases.

A great example is connecting to rust-lang.org on port 80. Rust users would
need to:

  1. resolve the IP address of rust-lang.org using
     io::net::addrinfo::get_host_addresses.

  2. check for errors

  3. if all went well, use the returned IP address and the port number
     to construct a SocketAddr

  4. pass this SocketAddr to TcpStream::connect.

I'm modifying the type signature of TcpStream::connect and
TcpListener::bind so that the API is a little easier to use.

TcpStream::connect now accepts two arguments: a string describing the
host/IP of the host we wish to connect to, and a u16 representing the
remote port number.

Similarly, TcpListener::bind has been modified to take two arguments:
a string describing the local interface address (e.g. "0.0.0.0" or
"127.0.0.1") and a u16 port number.

Here's how to port your Rust code to use the new TcpStream::connect API:

  // old ::connect API
  let addr = SocketAddr{ip: Ipv4Addr{127, 0, 0, 1}, port: 8080};
  let stream = TcpStream::connect(addr).unwrap()

  // new ::connect API (minimal change)
  let addr = SocketAddr{ip: Ipv4Addr{127, 0, 0, 1}, port: 8080};
  let stream = TcpStream::connect(addr.ip.to_str(), addr.port()).unwrap()

  // new ::connect API (more compact)
  let stream = TcpStream::connect("127.0.0.1", 8080).unwrap()

  // new ::connect API (hostname)
  let stream = TcpStream::connect("rust-lang.org", 80)

Similarly, for TcpListener::bind:

  // old ::bind API
  let addr = SocketAddr{ip: Ipv4Addr{0, 0, 0, 0}, port: 8080};
  let mut acceptor = TcpListener::bind(addr).listen();

  // new ::bind API (minimal change)
  let addr = SocketAddr{ip: Ipv4Addr{0, 0, 0, 0}, port: 8080};
  let mut acceptor = TcpListener::bind(addr.ip.to_str(), addr.port()).listen()

  // new ::bind API (more compact)
  let mut acceptor = TcpListener::bind("0.0.0.0", 8080).listen()

[breaking-change]
2014-05-12 21:41:48 -07:00
..
comm core: Remove the cast module 2014-05-11 01:13:02 -07:00
fmt librustc: Remove all uses of ~str from librustc. 2014-05-12 11:28:57 -07:00
hash core: Remove the cast module 2014-05-11 01:13:02 -07:00
io Easier interface for TCP ::connect and ::bind. 2014-05-12 21:41:48 -07:00
num core: Remove the cast module 2014-05-11 01:13:02 -07:00
path core: Remove the cast module 2014-05-11 01:13:02 -07:00
rt register snapshots 2014-05-12 02:52:32 -04:00
sync sync::deque: port to the new allocator API 2014-05-11 13:54:53 -04:00
unstable core: Remove the cast module 2014-05-11 01:13:02 -07:00
ascii.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
bitflags.rs
c_str.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
c_vec.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
cleanup.rs
from_str.rs core: Inherit the bool module 2014-05-07 08:14:56 -07:00
gc.rs core: Inhert ~/@/& cmp traits, remove old modules 2014-05-07 08:15:58 -07:00
lib.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
local_data.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
macros.rs librustc: Remove all uses of ~str from librustc. 2014-05-12 11:28:57 -07:00
option.rs Test fixes and rebase conflicts 2014-05-07 11:03:12 -07:00
os.rs auto merge of #14054 : luqmana/rust/at, r=alexcrichton 2014-05-09 12:56:35 -07:00
prelude.rs core: Move Option::expect to libstd from libcore 2014-05-07 08:17:32 -07:00
rc.rs heap: replace exchange_free with deallocate 2014-05-11 18:41:45 -04:00
reflect.rs Register new snapshots 2014-05-09 21:13:02 -07:00
repr.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
result.rs doc: Fix some broken links 2014-05-09 14:42:12 -07:00
rtdeps.rs
slice.rs register snapshots 2014-05-12 02:52:32 -04:00
str.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00
strbuf.rs auto merge of #14110 : SSheldon/rust/strbuf_mutable, r=alexcrichton 2014-05-11 16:21:44 -07:00
task.rs core: Add unwrap()/unwrap_err() methods to Result 2014-05-07 08:16:14 -07:00
to_str.rs core: Inherit possible string functionality 2014-05-07 08:16:14 -07:00
unicode.rs core: Inherit necessary unicode functionality 2014-05-07 08:15:58 -07:00
vec.rs core: Remove the cast module 2014-05-11 01:13:02 -07:00