Skip to content

Commit ac84674

Browse files
committed
Switched io::net::tcp to use ToSocketAddr
TcpListener and TcpStream are converted to use ToSocketAddr trait in their constructor methods. [breaking-change]
1 parent d97bfb2 commit ac84674

File tree

2 files changed

+130
-227
lines changed

2 files changed

+130
-227
lines changed

src/libstd/io/net/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
//! Networking I/O
1212
13+
use io::{IoError, InvalidInput};
14+
use option::None;
15+
use result::{Result, Ok, Err};
1316
use rt::rtio;
14-
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr};
17+
use self::ip::{Ipv4Addr, Ipv6Addr, IpAddr, ToSocketAddr};
1518

1619
pub use self::addrinfo::get_host_addresses;
1720

@@ -38,3 +41,25 @@ fn from_rtio(ip: rtio::IpAddr) -> IpAddr {
3841
}
3942
}
4043
}
44+
45+
fn with_addresses<A: ToSocketAddr, T>(
46+
addr: A,
47+
action: |&mut rtio::IoFactory, rtio::SocketAddr| -> Result<T, rtio::IoError>
48+
) -> Result<T, IoError> {
49+
const DEFAULT_ERROR: IoError = IoError {
50+
kind: InvalidInput,
51+
desc: "no addresses found for hostname",
52+
detail: None
53+
};
54+
55+
let addresses = try!(addr.to_socket_addr_all());
56+
let mut err = DEFAULT_ERROR;
57+
for addr in addresses.into_iter() {
58+
let addr = rtio::SocketAddr { ip: to_rtio(addr.ip), port: addr.port };
59+
match rtio::LocalIo::maybe_raise(|io| action(io, addr)) {
60+
Ok(r) => return Ok(r),
61+
Err(e) => err = IoError::from_rtio_error(e)
62+
}
63+
}
64+
Err(err)
65+
}

0 commit comments

Comments
 (0)