Seal ToSocketAddrs methods with an internal argument (#2892)

Closes #2891
This commit is contained in:
Sean McArthur 2020-09-28 14:43:41 -07:00 committed by GitHub
parent 078d0a2ebc
commit c6fc35aadf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 30 deletions

View File

@ -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)]

View File

@ -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
}
}

View File

@ -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! {

View File

@ -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;

View File

@ -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;

View File

@ -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,