From 92eb635669d966840d66067664196d5f8a407f5f Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 16 Nov 2019 15:12:57 +0900 Subject: [PATCH] net: add more impls for ToSocketAddrs (#1760) --- tokio/src/net/addr.rs | 69 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 9 deletions(-) diff --git a/tokio/src/net/addr.rs b/tokio/src/net/addr.rs index 33d30a22b..7c30a7e4a 100644 --- a/tokio/src/net/addr.rs +++ b/tokio/src/net/addr.rs @@ -1,9 +1,7 @@ use crate::future; use std::io; -use std::net::{IpAddr, SocketAddr}; -#[cfg(feature = "dns")] -use std::net::{Ipv4Addr, Ipv6Addr}; +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; /// Convert or resolve without blocking to one or more `SocketAddr` values. /// @@ -32,6 +30,32 @@ impl sealed::ToSocketAddrsPriv for SocketAddr { } } +// ===== impl SocketAddrV4 ===== + +impl ToSocketAddrs for SocketAddrV4 {} + +impl sealed::ToSocketAddrsPriv for SocketAddrV4 { + type Iter = std::option::IntoIter; + type Future = ReadyFuture; + + fn to_socket_addrs(&self) -> Self::Future { + SocketAddr::V4(*self).to_socket_addrs() + } +} + +// ===== impl SocketAddrV6 ===== + +impl ToSocketAddrs for SocketAddrV6 {} + +impl sealed::ToSocketAddrsPriv for SocketAddrV6 { + type Iter = std::option::IntoIter; + type Future = ReadyFuture; + + fn to_socket_addrs(&self) -> Self::Future { + SocketAddr::V6(*self).to_socket_addrs() + } +} + // ===== impl str ===== #[cfg(feature = "dns")] @@ -65,17 +89,16 @@ impl sealed::ToSocketAddrsPriv for str { // ===== impl (&str, u16) ===== #[cfg(feature = "dns")] -impl ToSocketAddrs for (&'_ str, u16) {} +impl ToSocketAddrs for (&str, u16) {} #[cfg(feature = "dns")] -impl sealed::ToSocketAddrsPriv for (&'_ str, u16) { +impl sealed::ToSocketAddrsPriv for (&str, u16) { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; fn to_socket_addrs(&self) -> Self::Future { use crate::blocking; use sealed::MaybeReady; - use std::net::{SocketAddrV4, SocketAddrV6}; let (host, port) = *self; @@ -116,6 +139,34 @@ impl sealed::ToSocketAddrsPriv for (IpAddr, u16) { } } +// ===== impl (Ipv4Addr, u16) ===== + +impl ToSocketAddrs for (Ipv4Addr, u16) {} + +impl sealed::ToSocketAddrsPriv for (Ipv4Addr, u16) { + type Iter = std::option::IntoIter; + type Future = ReadyFuture; + + fn to_socket_addrs(&self) -> Self::Future { + let (ip, port) = *self; + SocketAddrV4::new(ip, port).to_socket_addrs() + } +} + +// ===== impl (Ipv6Addr, u16) ===== + +impl ToSocketAddrs for (Ipv6Addr, u16) {} + +impl sealed::ToSocketAddrsPriv for (Ipv6Addr, u16) { + type Iter = std::option::IntoIter; + type Future = ReadyFuture; + + fn to_socket_addrs(&self) -> Self::Future { + let (ip, port) = *self; + SocketAddrV6::new(ip, port, 0, 0).to_socket_addrs() + } +} + // ===== impl String ===== #[cfg(feature = "dns")] @@ -131,11 +182,11 @@ impl sealed::ToSocketAddrsPriv for String { } } -// ===== impl &'_ impl ToSocketAddrs ===== +// ===== impl &impl ToSocketAddrs ===== -impl ToSocketAddrs for &'_ T {} +impl ToSocketAddrs for &T {} -impl sealed::ToSocketAddrsPriv for &'_ T +impl sealed::ToSocketAddrsPriv for &T where T: sealed::ToSocketAddrsPriv + ?Sized, {