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.
///
/// 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()?)
}
}