Skip to content

Commit 7e3344b

Browse files
committed
Migrated io::net::udp over to ToSocketAddr
UdpSocket constructor methods now use ToSocketAddr trait instead of SocketAddr. [breaking-change]
1 parent ac84674 commit 7e3344b

File tree

3 files changed

+34
-20
lines changed

3 files changed

+34
-20
lines changed

src/libstd/io/net/mod.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
//! Networking I/O
1212
13-
use io::{IoError, InvalidInput};
13+
use io::{IoError, IoResult, InvalidInput};
1414
use option::None;
1515
use result::{Result, Ok, Err};
1616
use rt::rtio;
17-
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, ToSocketAddr};
17+
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, SocketAddr, ToSocketAddr};
1818

1919
pub use self::addrinfo::get_host_addresses;
2020

@@ -42,7 +42,7 @@ fn from_rtio(ip: rtio::IpAddr) -> IpAddr {
4242
}
4343
}
4444

45-
fn with_addresses<A: ToSocketAddr, T>(
45+
fn with_addresses_io<A: ToSocketAddr, T>(
4646
addr: A,
4747
action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
4848
) -> Result<T, IoError> {
@@ -63,3 +63,22 @@ fn with_addresses<A: ToSocketAddr, T>(
6363
}
6464
Err(err)
6565
}
66+
67+
fn with_addresses<A: ToSocketAddr, T>(addr: A, action: |SocketAddr| -> IoResult<T>)
68+
-> IoResult<T> {
69+
const DEFAULT_ERROR: IoError = IoError {
70+
kind: InvalidInput,
71+
desc: "no addresses found for hostname",
72+
detail: None
73+
};
74+
75+
let addresses = try!(addr.to_socket_addr_all());
76+
let mut err = DEFAULT_ERROR;
77+
for addr in addresses.into_iter() {
78+
match action(addr) {
79+
Ok(r) => return Ok(r),
80+
Err(e) => err = e
81+
}
82+
}
83+
Err(err)
84+
}

src/libstd/io/net/tcp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl TcpStream {
6767
/// trait can be supplied for the address; see this trait documentation for
6868
/// concrete examples.
6969
pub fn connect<A: ToSocketAddr>(addr: A) -> IoResult<TcpStream> {
70-
super::with_addresses(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
70+
super::with_addresses_io(addr, |io, addr| io.tcp_connect(addr, None).map(TcpStream::new))
7171
}
7272

7373
/// Creates a TCP connection to a remote socket address, timing out after
@@ -89,7 +89,7 @@ impl TcpStream {
8989
return Err(standard_error(TimedOut));
9090
}
9191

92-
super::with_addresses(addr, |io, addr|
92+
super::with_addresses_io(addr, |io, addr|
9393
io.tcp_connect(addr, Some(timeout.num_milliseconds() as u64)).map(TcpStream::new)
9494
)
9595
}
@@ -324,7 +324,7 @@ impl TcpListener {
324324
/// to this listener. The port allocated can be queried via the
325325
/// `socket_name` function.
326326
pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<TcpListener> {
327-
super::with_addresses(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
327+
super::with_addresses_io(addr, |io, addr| io.tcp_bind(addr).map(|l| TcpListener { obj: l }))
328328
}
329329

330330
/// Returns the local socket address of this listener.

src/libstd/io/net/udp.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! datagram protocol.
1717
1818
use clone::Clone;
19-
use io::net::ip::{SocketAddr, IpAddr};
19+
use io::net::ip::{SocketAddr, IpAddr, ToSocketAddr};
2020
use io::{Reader, Writer, IoResult, IoError};
2121
use kinds::Send;
2222
use boxed::Box;
@@ -65,18 +65,13 @@ pub struct UdpSocket {
6565

6666
impl UdpSocket {
6767
/// Creates a UDP socket from the given socket address.
68-
pub fn bind(addr: SocketAddr) -> IoResult<UdpSocket> {
69-
let SocketAddr { ip, port } = addr;
70-
LocalIo::maybe_raise(|io| {
71-
let addr = rtio::SocketAddr { ip: super::to_rtio(ip), port: port };
72-
io.udp_bind(addr).map(|s| UdpSocket { obj: s })
73-
}).map_err(IoError::from_rtio_error)
68+
pub fn bind<A: ToSocketAddr>(addr: A) -> IoResult<UdpSocket> {
69+
super::with_addresses_io(addr, |io, addr| io.udp_bind(addr).map(|s| UdpSocket { obj: s }))
7470
}
7571

7672
/// Receives data from the socket. On success, returns the number of bytes
7773
/// read and the address from whence the data came.
78-
pub fn recv_from(&mut self, buf: &mut [u8])
79-
-> IoResult<(uint, SocketAddr)> {
74+
pub fn recv_from(&mut self, buf: &mut [u8]) -> IoResult<(uint, SocketAddr)> {
8075
match self.obj.recv_from(buf) {
8176
Ok((amt, rtio::SocketAddr { ip, port })) => {
8277
Ok((amt, SocketAddr { ip: super::from_rtio(ip), port: port }))
@@ -87,11 +82,11 @@ impl UdpSocket {
8782

8883
/// Sends data on the socket to the given address. Returns nothing on
8984
/// success.
90-
pub fn send_to(&mut self, buf: &[u8], dst: SocketAddr) -> IoResult<()> {
91-
self.obj.send_to(buf, rtio::SocketAddr {
92-
ip: super::to_rtio(dst.ip),
93-
port: dst.port,
94-
}).map_err(IoError::from_rtio_error)
85+
pub fn send_to<A: ToSocketAddr>(&mut self, buf: &[u8], addr: A) -> IoResult<()> {
86+
super::with_addresses(addr, |addr| self.obj.send_to(buf, rtio::SocketAddr {
87+
ip: super::to_rtio(addr.ip),
88+
port: addr.port,
89+
}).map_err(IoError::from_rtio_error))
9590
}
9691

9792
/// Creates a `UdpStream`, which allows use of the `Reader` and `Writer`

0 commit comments

Comments
 (0)