diff options
Diffstat (limited to 'src/net/addr.rs')
-rw-r--r-- | src/net/addr.rs | 112 |
1 files changed, 69 insertions, 43 deletions
diff --git a/src/net/addr.rs b/src/net/addr.rs index 5ba898a..7cbe531 100644 --- a/src/net/addr.rs +++ b/src/net/addr.rs @@ -9,7 +9,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV /// /// Implementations of `ToSocketAddrs` for string types require a DNS lookup. /// These implementations are only provided when Tokio is used with the -/// **`dns`** feature flag. +/// **`net`** feature flag. /// /// # Calling /// @@ -23,6 +23,15 @@ pub trait ToSocketAddrs: sealed::ToSocketAddrsPriv {} type ReadyFuture<T> = future::Ready<io::Result<T>>; +cfg_net! { + pub(crate) fn to_socket_addrs<T>(arg: T) -> T::Future + where + T: ToSocketAddrs, + { + arg.to_socket_addrs(sealed::Internal) + } +} + // ===== impl &impl ToSocketAddrs ===== impl<T: ToSocketAddrs + ?Sized> ToSocketAddrs for &T {} @@ -34,8 +43,8 @@ where type Iter = T::Iter; type Future = T::Future; - fn to_socket_addrs(&self) -> Self::Future { - (**self).to_socket_addrs() + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + (**self).to_socket_addrs(sealed::Internal) } } @@ -47,7 +56,7 @@ impl sealed::ToSocketAddrsPriv for SocketAddr { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { let iter = Some(*self).into_iter(); future::ok(iter) } @@ -61,8 +70,8 @@ impl sealed::ToSocketAddrsPriv for SocketAddrV4 { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { - SocketAddr::V4(*self).to_socket_addrs() + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + SocketAddr::V4(*self).to_socket_addrs(sealed::Internal) } } @@ -74,8 +83,8 @@ impl sealed::ToSocketAddrsPriv for SocketAddrV6 { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { - SocketAddr::V6(*self).to_socket_addrs() + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + SocketAddr::V6(*self).to_socket_addrs(sealed::Internal) } } @@ -87,7 +96,7 @@ impl sealed::ToSocketAddrsPriv for (IpAddr, u16) { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { let iter = Some(SocketAddr::from(*self)).into_iter(); future::ok(iter) } @@ -101,9 +110,9 @@ impl sealed::ToSocketAddrsPriv for (Ipv4Addr, u16) { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { let (ip, port) = *self; - SocketAddrV4::new(ip, port).to_socket_addrs() + SocketAddrV4::new(ip, port).to_socket_addrs(sealed::Internal) } } @@ -115,9 +124,9 @@ impl sealed::ToSocketAddrsPriv for (Ipv6Addr, u16) { type Iter = std::option::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { let (ip, port) = *self; - SocketAddrV6::new(ip, port, 0, 0).to_socket_addrs() + SocketAddrV6::new(ip, port, 0, 0).to_socket_addrs(sealed::Internal) } } @@ -129,13 +138,13 @@ impl sealed::ToSocketAddrsPriv for &[SocketAddr] { type Iter = std::vec::IntoIter<SocketAddr>; type Future = ReadyFuture<Self::Iter>; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { let iter = self.to_vec().into_iter(); future::ok(iter) } } -cfg_dns! { +cfg_net! { // ===== impl str ===== impl ToSocketAddrs for str {} @@ -144,23 +153,23 @@ cfg_dns! { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; - fn to_socket_addrs(&self) -> Self::Future { - use crate::runtime::spawn_blocking; + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + use crate::blocking::spawn_blocking; use sealed::MaybeReady; // First check if the input parses as a socket address let res: Result<SocketAddr, _> = self.parse(); if let Ok(addr) = res { - return MaybeReady::Ready(Some(addr)); + return MaybeReady(sealed::State::Ready(Some(addr))); } // Run DNS lookup on the blocking pool let s = self.to_owned(); - MaybeReady::Blocking(spawn_blocking(move || { + MaybeReady(sealed::State::Blocking(spawn_blocking(move || { std::net::ToSocketAddrs::to_socket_addrs(&s) - })) + }))) } } @@ -172,8 +181,8 @@ cfg_dns! { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; - fn to_socket_addrs(&self) -> Self::Future { - use crate::runtime::spawn_blocking; + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + use crate::blocking::spawn_blocking; use sealed::MaybeReady; let (host, port) = *self; @@ -183,21 +192,34 @@ cfg_dns! { let addr = SocketAddrV4::new(addr, port); let addr = SocketAddr::V4(addr); - return MaybeReady::Ready(Some(addr)); + return MaybeReady(sealed::State::Ready(Some(addr))); } if let Ok(addr) = host.parse::<Ipv6Addr>() { let addr = SocketAddrV6::new(addr, port, 0, 0); let addr = SocketAddr::V6(addr); - return MaybeReady::Ready(Some(addr)); + return MaybeReady(sealed::State::Ready(Some(addr))); } let host = host.to_owned(); - MaybeReady::Blocking(spawn_blocking(move || { + MaybeReady(sealed::State::Blocking(spawn_blocking(move || { std::net::ToSocketAddrs::to_socket_addrs(&(&host[..], port)) - })) + }))) + } + } + + // ===== impl (String, u16) ===== + + impl ToSocketAddrs for (String, u16) {} + + impl sealed::ToSocketAddrsPriv for (String, u16) { + type Iter = sealed::OneOrMore; + type Future = sealed::MaybeReady; + + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + (self.0.as_str(), self.1).to_socket_addrs(sealed::Internal) } } @@ -209,8 +231,8 @@ cfg_dns! { type Iter = <str as sealed::ToSocketAddrsPriv>::Iter; type Future = <str as sealed::ToSocketAddrsPriv>::Future; - fn to_socket_addrs(&self) -> Self::Future { - (&self[..]).to_socket_addrs() + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + (&self[..]).to_socket_addrs(sealed::Internal) } } } @@ -224,27 +246,31 @@ pub(crate) mod sealed { use std::io; use std::net::SocketAddr; - cfg_dns! { - use crate::task::JoinHandle; - - use std::option; - use std::pin::Pin; - use std::task::{Context, Poll}; - use std::vec; - } - #[doc(hidden)] pub trait ToSocketAddrsPriv { type Iter: Iterator<Item = SocketAddr> + Send + 'static; type Future: Future<Output = io::Result<Self::Iter>> + Send + 'static; - fn to_socket_addrs(&self) -> Self::Future; + fn to_socket_addrs(&self, internal: Internal) -> Self::Future; } - cfg_dns! { + #[allow(missing_debug_implementations)] + pub struct Internal; + + cfg_net! { + use crate::blocking::JoinHandle; + + use std::option; + use std::pin::Pin; + use std::task::{Context, Poll}; + use std::vec; + #[doc(hidden)] #[derive(Debug)] - pub enum MaybeReady { + pub struct MaybeReady(pub(super) State); + + #[derive(Debug)] + pub(super) enum State { Ready(Option<SocketAddr>), Blocking(JoinHandle<io::Result<vec::IntoIter<SocketAddr>>>), } @@ -260,12 +286,12 @@ pub(crate) mod sealed { type Output = io::Result<OneOrMore>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { - match *self { - MaybeReady::Ready(ref mut i) => { + match self.0 { + State::Ready(ref mut i) => { let iter = OneOrMore::One(i.take().into_iter()); Poll::Ready(Ok(iter)) } - MaybeReady::Blocking(ref mut rx) => { + State::Blocking(ref mut rx) => { let res = ready!(Pin::new(rx).poll(cx))?.map(OneOrMore::More); Poll::Ready(res) |