Rollup merge of #39372 - seanmonstar:more-addr-froms, r=alexcrichton
A few ergonomic From impls for SocketAddr/IpAddr My main motivation is removing things like this: `"127.0.0.1:3000".parse().unwrap()`. Instead, this now works: `SocketAddr::from(([127, 0, 0, 1], 3000))` or even `([127, 0, 0, 1], 3000).into())` when passing to a function.
This commit is contained in:
@@ -456,6 +456,13 @@ impl From<SocketAddrV6> for SocketAddr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "addr_from_into_ip", since = "1.17.0")]
|
||||||
|
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr {
|
||||||
|
fn from(pieces: (I, u16)) -> SocketAddr {
|
||||||
|
SocketAddr::new(pieces.0.into(), pieces.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> IntoInner<(*const c::sockaddr, c::socklen_t)> for &'a SocketAddr {
|
impl<'a> IntoInner<(*const c::sockaddr, c::socklen_t)> for &'a SocketAddr {
|
||||||
fn into_inner(self) -> (*const c::sockaddr, c::socklen_t) {
|
fn into_inner(self) -> (*const c::sockaddr, c::socklen_t) {
|
||||||
match *self {
|
match *self {
|
||||||
|
|||||||
@@ -656,6 +656,13 @@ impl From<[u8; 4]> for Ipv4Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||||
|
impl From<[u8; 4]> for IpAddr {
|
||||||
|
fn from(octets: [u8; 4]) -> IpAddr {
|
||||||
|
IpAddr::V4(Ipv4Addr::from(octets))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Ipv6Addr {
|
impl Ipv6Addr {
|
||||||
/// Creates a new IPv6 address from eight 16-bit segments.
|
/// Creates a new IPv6 address from eight 16-bit segments.
|
||||||
///
|
///
|
||||||
@@ -1186,6 +1193,21 @@ impl From<[u16; 8]> for Ipv6Addr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||||
|
impl From<[u8; 16]> for IpAddr {
|
||||||
|
fn from(octets: [u8; 16]) -> IpAddr {
|
||||||
|
IpAddr::V6(Ipv6Addr::from(octets))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[stable(feature = "ip_from_slice", since = "1.17.0")]
|
||||||
|
impl From<[u16; 8]> for IpAddr {
|
||||||
|
fn from(segments: [u16; 8]) -> IpAddr {
|
||||||
|
IpAddr::V6(Ipv6Addr::from(segments))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Tests for this module
|
// Tests for this module
|
||||||
#[cfg(all(test, not(target_os = "emscripten")))]
|
#[cfg(all(test, not(target_os = "emscripten")))]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|||||||
Reference in New Issue
Block a user