From 798aa194095c1abb0e2d0bfb1f581720727eea9f Mon Sep 17 00:00:00 2001 From: DCjanus Date: Tue, 20 Aug 2019 21:35:31 +0800 Subject: [PATCH] replace `std::net::ToSocketAddrs` with `async-std::net::ToSocketAddrs` --- src/net/tcp/listener.rs | 10 +++++----- src/net/tcp/stream.rs | 10 +++++----- src/net/udp/mod.rs | 15 ++++++++------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/net/tcp/listener.rs b/src/net/tcp/listener.rs index e85d90b7..b0d4b58c 100644 --- a/src/net/tcp/listener.rs +++ b/src/net/tcp/listener.rs @@ -1,4 +1,3 @@ -use std::net::{self, SocketAddr, ToSocketAddrs}; use std::pin::Pin; use cfg_if::cfg_if; @@ -8,6 +7,7 @@ use super::TcpStream; use crate::future::Future; use crate::io; use crate::net::driver::IoHandle; +use crate::net::{SocketAddr, ToSocketAddrs}; use crate::task::{Context, Poll}; /// A TCP socket server, listening for connections. @@ -84,7 +84,7 @@ impl TcpListener { pub async fn bind(addrs: A) -> io::Result { let mut last_err = None; - for addr in addrs.to_socket_addrs()? { + for addr in addrs.to_socket_addrs().await? { match mio::net::TcpListener::bind(&addr) { Ok(mio_listener) => { #[cfg(unix)] @@ -241,9 +241,9 @@ impl<'a> futures::Stream for Incoming<'a> { } } -impl From for TcpListener { +impl From for TcpListener { /// Converts a `std::net::TcpListener` into its asynchronous equivalent. - fn from(listener: net::TcpListener) -> TcpListener { + fn from(listener: std::net::TcpListener) -> TcpListener { let mio_listener = mio::net::TcpListener::from_std(listener).unwrap(); #[cfg(unix)] @@ -284,7 +284,7 @@ cfg_if! { impl FromRawFd for TcpListener { unsafe fn from_raw_fd(fd: RawFd) -> TcpListener { - net::TcpListener::from_raw_fd(fd).into() + std::net::TcpListener::from_raw_fd(fd).into() } } diff --git a/src/net/tcp/stream.rs b/src/net/tcp/stream.rs index 585d1ae0..c189680c 100644 --- a/src/net/tcp/stream.rs +++ b/src/net/tcp/stream.rs @@ -1,6 +1,5 @@ use std::io::{IoSlice, IoSliceMut}; use std::mem; -use std::net::{self, SocketAddr, ToSocketAddrs}; use std::pin::Pin; use cfg_if::cfg_if; @@ -9,6 +8,7 @@ use futures::io::{AsyncRead, AsyncWrite}; use crate::io; use crate::net::driver::IoHandle; +use crate::net::{SocketAddr, ToSocketAddrs}; use crate::task::{Context, Poll}; /// A TCP stream between a local and a remote socket. @@ -88,7 +88,7 @@ impl TcpStream { let mut last_err = None; - for addr in addrs.to_socket_addrs()? { + for addr in addrs.to_socket_addrs().await? { let mut state = { match mio::net::TcpStream::connect(&addr) { Ok(mio_stream) => { @@ -444,9 +444,9 @@ impl AsyncWrite for &TcpStream { } } -impl From for TcpStream { +impl From for TcpStream { /// Converts a `std::net::TcpStream` into its asynchronous equivalent. - fn from(stream: net::TcpStream) -> TcpStream { + fn from(stream: std::net::TcpStream) -> TcpStream { let mio_stream = mio::net::TcpStream::from_stream(stream).unwrap(); #[cfg(unix)] @@ -487,7 +487,7 @@ cfg_if! { impl FromRawFd for TcpStream { unsafe fn from_raw_fd(fd: RawFd) -> TcpStream { - net::TcpStream::from_raw_fd(fd).into() + std::net::TcpStream::from_raw_fd(fd).into() } } diff --git a/src/net/udp/mod.rs b/src/net/udp/mod.rs index 04f1c55f..bbc4390f 100644 --- a/src/net/udp/mod.rs +++ b/src/net/udp/mod.rs @@ -1,9 +1,10 @@ use std::io; -use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs}; use cfg_if::cfg_if; use futures::future; +use std::net::{Ipv4Addr, Ipv6Addr}; +use crate::net::addr::{SocketAddr, ToSocketAddrs}; use crate::net::driver::IoHandle; use crate::task::Poll; @@ -77,7 +78,7 @@ impl UdpSocket { pub async fn bind(addr: A) -> io::Result { let mut last_err = None; - for addr in addr.to_socket_addrs()? { + for addr in addr.to_socket_addrs().await? { match mio::net::UdpSocket::bind(&addr) { Ok(mio_socket) => { #[cfg(unix)] @@ -156,7 +157,7 @@ impl UdpSocket { /// # Ok(()) }) } /// ``` pub async fn send_to(&self, buf: &[u8], addrs: A) -> io::Result { - let addr = match addrs.to_socket_addrs()?.next() { + let addr = match addrs.to_socket_addrs().await?.next() { Some(addr) => addr, None => { return Err(io::Error::new( @@ -243,7 +244,7 @@ impl UdpSocket { pub async fn connect(&self, addrs: A) -> io::Result<()> { let mut last_err = None; - for addr in addrs.to_socket_addrs()? { + for addr in addrs.to_socket_addrs().await? { match self.io_handle.get_ref().connect(addr) { Ok(()) => return Ok(()), Err(err) => last_err = Some(err), @@ -516,9 +517,9 @@ impl UdpSocket { } } -impl From for UdpSocket { +impl From for UdpSocket { /// Converts a `std::net::UdpSocket` into its asynchronous equivalent. - fn from(socket: net::UdpSocket) -> UdpSocket { + fn from(socket: std::net::UdpSocket) -> UdpSocket { let mio_socket = mio::net::UdpSocket::from_socket(socket).unwrap(); #[cfg(unix)] @@ -559,7 +560,7 @@ cfg_if! { impl FromRawFd for UdpSocket { unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket { - net::UdpSocket::from_raw_fd(fd).into() + std::net::UdpSocket::from_raw_fd(fd).into() } }