use option;
use sys::net::netc as c;
use sys_common::{FromInner, AsInner, IntoInner};
-use sys_common::net::lookup_host;
+use sys_common::net::LookupHost;
use vec;
use iter;
use slice;
+use convert::TryInto;
/// An internet socket address, either IPv4 or IPv6.
///
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<SocketAddrV4> for SocketAddr {
+ /// Converts a [`SocketAddrV4`] into a [`SocketAddr::V4`].
fn from(sock4: SocketAddrV4) -> SocketAddr {
SocketAddr::V4(sock4)
}
#[stable(feature = "ip_from_ip", since = "1.16.0")]
impl From<SocketAddrV6> for SocketAddr {
+ /// Converts a [`SocketAddrV6`] into a [`SocketAddr::V6`].
fn from(sock6: SocketAddrV6) -> SocketAddr {
SocketAddr::V6(sock6)
}
#[stable(feature = "addr_from_into_ip", since = "1.17.0")]
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
+ /// Converts a tuple struct (Into<[`IpAddr`]>, `u16`) into a [`SocketAddr`].
+ ///
+ /// This conversion creates a [`SocketAddr::V4`] for a [`IpAddr::V4`]
+ /// and creates a [`SocketAddr::V6`] for a [`IpAddr::V6`].
+ ///
+ /// `u16` is treated as port of the newly created [`SocketAddr`].
fn from(pieces: (I, u16)) -> SocketAddr {
SocketAddr::new(pieces.0.into(), pieces.1)
}
/// the other: for simple uses a string like `"localhost:12345"` is much nicer
/// than manual construction of the corresponding [`SocketAddr`], but sometimes
/// [`SocketAddr`] value is *the* main source of the address, and converting it to
-/// some other type (e.g. a string) just for it to be converted back to
+/// some other type (e.g., a string) just for it to be converted back to
/// [`SocketAddr`] in constructor methods is pointless.
///
/// Addresses returned by the operating system that are not IP addresses are
}
}
-fn resolve_socket_addr(s: &str, p: u16) -> io::Result<vec::IntoIter<SocketAddr>> {
- let ips = lookup_host(s)?;
- let v: Vec<_> = ips.map(|mut a| { a.set_port(p); a }).collect();
+fn resolve_socket_addr(lh: LookupHost) -> io::Result<vec::IntoIter<SocketAddr>> {
+ let p = lh.port();
+ let v: Vec<_> = lh.map(|mut a| { a.set_port(p); a }).collect();
Ok(v.into_iter())
}
return Ok(vec![SocketAddr::V6(addr)].into_iter())
}
- resolve_socket_addr(host, port)
+ resolve_socket_addr((host, port).try_into()?)
}
}
return Ok(vec![addr].into_iter());
}
- macro_rules! try_opt {
- ($e:expr, $msg:expr) => (
- match $e {
- Some(r) => r,
- None => return Err(io::Error::new(io::ErrorKind::InvalidInput,
- $msg)),
- }
- )
- }
-
- // split the string by ':' and convert the second part to u16
- let mut parts_iter = self.rsplitn(2, ':');
- let port_str = try_opt!(parts_iter.next(), "invalid socket address");
- let host = try_opt!(parts_iter.next(), "invalid socket address");
- let port: u16 = try_opt!(port_str.parse().ok(), "invalid port value");
- resolve_socket_addr(host, port)
+ resolve_socket_addr(self.try_into()?)
}
}