diff options
author | Sean McArthur <sean@seanmonstar.com> | 2020-09-28 14:43:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-28 14:43:41 -0700 |
commit | c6fc35aadf2cfb65e51d4a6ff5e56faf1599b670 (patch) | |
tree | 1974d433a2f2c1632ece3ec1b5cbbca89bbf47ed /tokio | |
parent | 078d0a2ebc4d4f88cb6bce05c8ac4f5038dae9be (diff) |
Seal ToSocketAddrs methods with an internal argument (#2892)
Closes #2891
Diffstat (limited to 'tokio')
-rw-r--r-- | tokio/src/net/addr.rs | 51 | ||||
-rw-r--r-- | tokio/src/net/lookup_host.rs | 4 | ||||
-rw-r--r-- | tokio/src/net/mod.rs | 2 | ||||
-rw-r--r-- | tokio/src/net/tcp/listener.rs | 4 | ||||
-rw-r--r-- | tokio/src/net/tcp/stream.rs | 4 | ||||
-rw-r--r-- | tokio/src/net/udp/socket.rs | 8 |
6 files changed, 43 insertions, 30 deletions
diff --git a/tokio/src/net/addr.rs b/tokio/src/net/addr.rs index ed00ca4a..86b0962b 100644 --- a/tokio/src/net/addr.rs +++ b/tokio/src/net/addr.rs @@ -23,6 +23,14 @@ pub trait ToSocketAddrs: sealed::ToSocketAddrsPriv {} type ReadyFuture<T> = future::Ready<io::Result<T>>; +#[cfg(any(feature = "dns", feature = "tcp", feature = "udp"))] +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 +42,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 +55,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 +69,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 +82,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 +95,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 +109,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 +123,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,7 +137,7 @@ 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) } @@ -144,7 +152,7 @@ cfg_dns! { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { use crate::runtime::spawn_blocking; use sealed::MaybeReady; @@ -172,7 +180,7 @@ cfg_dns! { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; - fn to_socket_addrs(&self) -> Self::Future { + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { use crate::runtime::spawn_blocking; use sealed::MaybeReady; @@ -209,8 +217,8 @@ cfg_dns! { type Iter = sealed::OneOrMore; type Future = sealed::MaybeReady; - fn to_socket_addrs(&self) -> Self::Future { - (self.0.as_str(), self.1).to_socket_addrs() + fn to_socket_addrs(&self, _: sealed::Internal) -> Self::Future { + (self.0.as_str(), self.1).to_socket_addrs(sealed::Internal) } } @@ -222,8 +230,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) } } } @@ -251,9 +259,12 @@ pub(crate) mod sealed { 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; } + #[allow(missing_debug_implementations)] + pub struct Internal; + cfg_dns! { #[doc(hidden)] #[derive(Debug)] diff --git a/tokio/src/net/lookup_host.rs b/tokio/src/net/lookup_host.rs index 3098b463..150228b5 100644 --- a/tokio/src/net/lookup_host.rs +++ b/tokio/src/net/lookup_host.rs @@ -1,5 +1,5 @@ cfg_dns! { - use crate::net::addr::ToSocketAddrs; + use crate::net::addr::{self, ToSocketAddrs}; use std::io; use std::net::SocketAddr; @@ -33,6 +33,6 @@ cfg_dns! { where T: ToSocketAddrs { - host.to_socket_addrs().await + addr::to_socket_addrs(host).await } } diff --git a/tokio/src/net/mod.rs b/tokio/src/net/mod.rs index da6ad1fc..e3fb2c73 100644 --- a/tokio/src/net/mod.rs +++ b/tokio/src/net/mod.rs @@ -23,6 +23,8 @@ //! [`UnixDatagram`]: UnixDatagram mod addr; +#[cfg(any(feature = "tcp", feature = "udp"))] +pub(crate) use addr::to_socket_addrs; pub use addr::ToSocketAddrs; cfg_dns! { diff --git a/tokio/src/net/tcp/listener.rs b/tokio/src/net/tcp/listener.rs index 323b8bca..ef0a87c0 100644 --- a/tokio/src/net/tcp/listener.rs +++ b/tokio/src/net/tcp/listener.rs @@ -1,7 +1,7 @@ use crate::future::poll_fn; use crate::io::PollEvented; use crate::net::tcp::{Incoming, TcpStream}; -use crate::net::ToSocketAddrs; +use crate::net::{to_socket_addrs, ToSocketAddrs}; use std::convert::TryFrom; use std::fmt; @@ -130,7 +130,7 @@ impl TcpListener { /// } /// ``` pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> { - let addrs = addr.to_socket_addrs().await?; + let addrs = to_socket_addrs(addr).await?; let mut last_err = None; diff --git a/tokio/src/net/tcp/stream.rs b/tokio/src/net/tcp/stream.rs index 4bf17449..99f77d41 100644 --- a/tokio/src/net/tcp/stream.rs +++ b/tokio/src/net/tcp/stream.rs @@ -2,7 +2,7 @@ use crate::future::poll_fn; use crate::io::{AsyncRead, AsyncWrite, PollEvented, ReadBuf}; use crate::net::tcp::split::{split, ReadHalf, WriteHalf}; use crate::net::tcp::split_owned::{split_owned, OwnedReadHalf, OwnedWriteHalf}; -use crate::net::ToSocketAddrs; +use crate::net::{to_socket_addrs, ToSocketAddrs}; use std::convert::TryFrom; use std::fmt; @@ -116,7 +116,7 @@ impl TcpStream { /// [`write_all`]: fn@crate::io::AsyncWriteExt::write_all /// [`AsyncWriteExt`]: trait@crate::io::AsyncWriteExt pub async fn connect<A: ToSocketAddrs>(addr: A) -> io::Result<TcpStream> { - let addrs = addr.to_socket_addrs().await?; + let addrs = to_socket_addrs(addr).await?; let mut last_err = None; diff --git a/tokio/src/net/udp/socket.rs b/tokio/src/net/udp/socket.rs index d0dece3e..1b943184 100644 --- a/tokio/src/net/udp/socket.rs +++ b/tokio/src/net/udp/socket.rs @@ -1,5 +1,5 @@ use crate::io::PollEvented; -use crate::net::ToSocketAddrs; +use crate::net::{to_socket_addrs, ToSocketAddrs}; use std::convert::TryFrom; use std::fmt; @@ -17,7 +17,7 @@ impl UdpSocket { /// This function will create a new UDP socket and attempt to bind it to /// the `addr` provided. pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> { - let addrs = addr.to_socket_addrs().await?; + let addrs = to_socket_addrs(addr).await?; let mut last_err = None; for addr in addrs { @@ -76,7 +76,7 @@ impl UdpSocket { /// limiting packets that are read via recv from the address specified in /// `addr`. pub async fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> { - let addrs = addr.to_socket_addrs().await?; + let addrs = to_socket_addrs(addr).await?; let mut last_err = None; for addr in addrs { @@ -145,7 +145,7 @@ impl UdpSocket { /// The future will resolve to an error if the IP version of the socket does /// not match that of `target`. pub async fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], target: A) -> io::Result<usize> { - let mut addrs = target.to_socket_addrs().await?; + let mut addrs = to_socket_addrs(target).await?; match addrs.next() { Some(target) => self.send_to_addr(buf, &target).await, |