about summary refs log tree commit diff
path: root/src/libstd/io
diff options
context:
space:
mode:
authorVladimir Matveev <vladimir.matweev@gmail.com>2014-10-29 22:59:43 +0300
committerVladimir Matveev <vladimir.matweev@gmail.com>2014-11-05 12:01:23 +0300
commit7e3344b17f777f5bca0d3eaa9278fa2d628ca064 (patch)
tree89bbab5cbe0ab28446f554cd77d637db8c1dadcc /src/libstd/io
parentac846749f0abbd0b6107406ba2f97886605e1ad4 (diff)
downloadrust-7e3344b17f777f5bca0d3eaa9278fa2d628ca064.tar.gz
rust-7e3344b17f777f5bca0d3eaa9278fa2d628ca064.zip
Migrated io::net::udp over to ToSocketAddr
UdpSocket constructor methods now use ToSocketAddr trait instead of
SocketAddr.

[breaking-change]
Diffstat (limited to 'src/libstd/io')
-rw-r--r--src/libstd/io/net/mod.rs25
-rw-r--r--src/libstd/io/net/tcp.rs6
-rw-r--r--src/libstd/io/net/udp.rs23
3 files changed, 34 insertions, 20 deletions
diff --git a/src/libstd/io/net/mod.rs b/src/libstd/io/net/mod.rs
index 68577e192d2..61cefa6f208 100644
--- a/src/libstd/io/net/mod.rs
+++ b/src/libstd/io/net/mod.rs
@@ -10,11 +10,11 @@
 
 //! Networking I/O
 
-use io::{IoError, InvalidInput};
+use io::{IoError, IoResult, InvalidInput};
 use option::None;
 use result::{Result, Ok, Err};
 use rt::rtio;
-use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, ToSocketAddr};
+use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, SocketAddr, ToSocketAddr};
 
 pub use self::addrinfo::get_host_addresses;
 
@@ -42,7 +42,7 @@ fn from_rtio(ip: rtio::IpAddr) -> IpAddr {
     }
 }
 
-fn with_addresses<A: ToSocketAddr, T>(
+fn with_addresses_io<A: ToSocketAddr, T>(
     addr: A, 
     action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
 ) -> Result<T, IoError> {
@@ -63,3 +63,22 @@ fn with_addresses<A: ToSocketAddr, T>(
     }
     Err(err)
 }
+
+fn with_addresses<A: ToSocketAddr, T>(addr: A, action: |SocketAddr| -> IoResult<T>)
+    -> IoResult<T> {
+    const DEFAULT_ERROR: IoError = IoError {
+        kind: InvalidInput,
+        desc: "no addresses found for hostname",
+        detail: None
+    };
+
+    let addresses = try!(addr.to_socket_addr_all());
+    let mut err = DEFAULT_ERROR;
+    for addr in addresses.into_iter() {
+        match action(addr) {
+            Ok(r) => return Ok(r),
+            Err(e) => err = e
+        }
+    }
+    Err(err)
+}
diff --git a/src/libstd/io/net/tcp.rs b/src/libstd/io/net/tcp.rs
index 5dffc45bcd8..ee5f56469ad 100644
--- a/src/libstd/io/net/tcp.rs
+++ b/src/libstd/io/net/tcp.rs
@@ -67,7 +67,7 @@ impl TcpStream {
     /// trait can be supplied for the address; see this trait documentation for
     /// concrete examples.
     pub fn connect<A: ToSocketAddr>(addr: A) -> IoResult<TcpStream> {
-        super::with_addresses(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
+        super::with_addresses_io(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
     }
 
     /// Creates a TCP connection to a remote socket address, timing out after
@@ -89,7 +89,7 @@ impl TcpStream {
             return Err(standard_error(TimedOut));
         }
 
-        super::with_addresses(addr, |io, addr|
+        super::with_addresses_io(addr, |io, addr|
             io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
         )
     }
@@ -324,7 +324,7 @@ impl TcpListener {
     /// to this listener. The port allocated can be queried via the
     /// `socket_name` function.
     pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<TcpListener> {
-        super::with_addresses(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
+        super::with_addresses_io(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
     }
 
     /// Returns the local socket address of this listener.
diff --git a/src/libstd/io/net/udp.rs b/src/libstd/io/net/udp.rs
index ad9ed090a5b..62eeba7cbd2 100644
--- a/src/libstd/io/net/udp.rs
+++ b/src/libstd/io/net/udp.rs
@@ -16,7 +16,7 @@
 //! datagram protocol.
 
 use clone::Clone;
-use io::net::ip::{SocketAddr, IpAddr};
+use io::net::ip::{SocketAddr, IpAddr, ToSocketAddr};
 use io::{Reader, Writer, IoResult, IoError};
 use kinds::Send;
 use boxed::Box;
@@ -65,18 +65,13 @@ pub struct UdpSocket {
 
 impl UdpSocket {
     /// Creates a UDP socket from the given socket address.
-    pub fn bind(addr: SocketAddr) -> IoResult<UdpSocket> {
-        let SocketAddr { ip, port } = addr;
-        LocalIo::maybe_raise(|io| {
-            let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
-            io.udp_bind(addr).map(|s| UdpSocket { obj: s })
-        }).map_err(IoError::from_rtio_error)
+    pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<UdpSocket> {
+        super::with_addresses_io(addr, |io, addr| io.udp_bind(addr).map(|s| UdpSocket { obj: s }))
     }
 
     /// Receives data from the socket. On success, returns the number of bytes
     /// read and the address from whence the data came.
-    pub fn recv_from(&mut self, buf: &mut [u8])
-                    -> IoResult<(uint, SocketAddr)> {
+    pub fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)> {
         match self.obj.recv_from(buf) {
             Ok((amt, rtio::SocketAddr { ip, port })) => {
                 Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
@@ -87,11 +82,11 @@ impl UdpSocket {
 
     /// Sends data on the socket to the given address. Returns nothing on
     /// success.
-    pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
-        self.obj.send_to(buf, rtio::SocketAddr {
-            ip: super::to_rtio(dst.ip),
-            port: dst.port,
-        }).map_err(IoError::from_rtio_error)
+    pub fn send_to<A: ToSocketAddr>(&mut self, buf: &[u8], addr: A) -> IoResult<()> {
+        super::with_addresses(addr, |addr| self.obj.send_to(buf, rtio::SocketAddr {
+            ip: super::to_rtio(addr.ip),
+            port: addr.port,
+        }).map_err(IoError::from_rtio_error))
     }
 
     /// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`