replace `std::net::ToSocketAddrs` with `async-std::net::ToSocketAddrs`

tosocketaddrs
DCjanus 5 years ago
parent d5e8d11fb9
commit 798aa19409

@ -1,4 +1,3 @@
use std::net::{self, SocketAddr, ToSocketAddrs};
use std::pin::Pin; use std::pin::Pin;
use cfg_if::cfg_if; use cfg_if::cfg_if;
@ -8,6 +7,7 @@ use super::TcpStream;
use crate::future::Future; use crate::future::Future;
use crate::io; use crate::io;
use crate::net::driver::IoHandle; use crate::net::driver::IoHandle;
use crate::net::{SocketAddr, ToSocketAddrs};
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
/// A TCP socket server, listening for connections. /// A TCP socket server, listening for connections.
@ -84,7 +84,7 @@ impl TcpListener {
pub async fn bind<A: ToSocketAddrs>(addrs: A) -> io::Result<TcpListener> { pub async fn bind<A: ToSocketAddrs>(addrs: A) -> io::Result<TcpListener> {
let mut last_err = None; 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) { match mio::net::TcpListener::bind(&addr) {
Ok(mio_listener) => { Ok(mio_listener) => {
#[cfg(unix)] #[cfg(unix)]
@ -241,9 +241,9 @@ impl<'a> futures::Stream for Incoming<'a> {
} }
} }
impl From<net::TcpListener> for TcpListener { impl From<std::net::TcpListener> for TcpListener {
/// Converts a `std::net::TcpListener` into its asynchronous equivalent. /// 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(); let mio_listener = mio::net::TcpListener::from_std(listener).unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -284,7 +284,7 @@ cfg_if! {
impl FromRawFd for TcpListener { impl FromRawFd for TcpListener {
unsafe fn from_raw_fd(fd: RawFd) -> TcpListener { unsafe fn from_raw_fd(fd: RawFd) -> TcpListener {
net::TcpListener::from_raw_fd(fd).into() std::net::TcpListener::from_raw_fd(fd).into()
} }
} }

@ -1,6 +1,5 @@
use std::io::{IoSlice, IoSliceMut}; use std::io::{IoSlice, IoSliceMut};
use std::mem; use std::mem;
use std::net::{self, SocketAddr, ToSocketAddrs};
use std::pin::Pin; use std::pin::Pin;
use cfg_if::cfg_if; use cfg_if::cfg_if;
@ -9,6 +8,7 @@ use futures::io::{AsyncRead, AsyncWrite};
use crate::io; use crate::io;
use crate::net::driver::IoHandle; use crate::net::driver::IoHandle;
use crate::net::{SocketAddr, ToSocketAddrs};
use crate::task::{Context, Poll}; use crate::task::{Context, Poll};
/// A TCP stream between a local and a remote socket. /// A TCP stream between a local and a remote socket.
@ -88,7 +88,7 @@ impl TcpStream {
let mut last_err = None; let mut last_err = None;
for addr in addrs.to_socket_addrs()? { for addr in addrs.to_socket_addrs().await? {
let mut state = { let mut state = {
match mio::net::TcpStream::connect(&addr) { match mio::net::TcpStream::connect(&addr) {
Ok(mio_stream) => { Ok(mio_stream) => {
@ -444,9 +444,9 @@ impl AsyncWrite for &TcpStream {
} }
} }
impl From<net::TcpStream> for TcpStream { impl From<std::net::TcpStream> for TcpStream {
/// Converts a `std::net::TcpStream` into its asynchronous equivalent. /// 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(); let mio_stream = mio::net::TcpStream::from_stream(stream).unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -487,7 +487,7 @@ cfg_if! {
impl FromRawFd for TcpStream { impl FromRawFd for TcpStream {
unsafe fn from_raw_fd(fd: RawFd) -> TcpStream { unsafe fn from_raw_fd(fd: RawFd) -> TcpStream {
net::TcpStream::from_raw_fd(fd).into() std::net::TcpStream::from_raw_fd(fd).into()
} }
} }

@ -1,9 +1,10 @@
use std::io; use std::io;
use std::net::{self, Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs};
use cfg_if::cfg_if; use cfg_if::cfg_if;
use futures::future; use futures::future;
use std::net::{Ipv4Addr, Ipv6Addr};
use crate::net::addr::{SocketAddr, ToSocketAddrs};
use crate::net::driver::IoHandle; use crate::net::driver::IoHandle;
use crate::task::Poll; use crate::task::Poll;
@ -77,7 +78,7 @@ impl UdpSocket {
pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> { pub async fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
let mut last_err = None; 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) { match mio::net::UdpSocket::bind(&addr) {
Ok(mio_socket) => { Ok(mio_socket) => {
#[cfg(unix)] #[cfg(unix)]
@ -156,7 +157,7 @@ impl UdpSocket {
/// # Ok(()) }) } /// # Ok(()) }) }
/// ``` /// ```
pub async fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addrs: A) -> io::Result<usize> { pub async fn send_to<A: ToSocketAddrs>(&self, buf: &[u8], addrs: A) -> io::Result<usize> {
let addr = match addrs.to_socket_addrs()?.next() { let addr = match addrs.to_socket_addrs().await?.next() {
Some(addr) => addr, Some(addr) => addr,
None => { None => {
return Err(io::Error::new( return Err(io::Error::new(
@ -243,7 +244,7 @@ impl UdpSocket {
pub async fn connect<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<()> { pub async fn connect<A: ToSocketAddrs>(&self, addrs: A) -> io::Result<()> {
let mut last_err = None; 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) { match self.io_handle.get_ref().connect(addr) {
Ok(()) => return Ok(()), Ok(()) => return Ok(()),
Err(err) => last_err = Some(err), Err(err) => last_err = Some(err),
@ -516,9 +517,9 @@ impl UdpSocket {
} }
} }
impl From<net::UdpSocket> for UdpSocket { impl From<std::net::UdpSocket> for UdpSocket {
/// Converts a `std::net::UdpSocket` into its asynchronous equivalent. /// 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(); let mio_socket = mio::net::UdpSocket::from_socket(socket).unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -559,7 +560,7 @@ cfg_if! {
impl FromRawFd for UdpSocket { impl FromRawFd for UdpSocket {
unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket { unsafe fn from_raw_fd(fd: RawFd) -> UdpSocket {
net::UdpSocket::from_raw_fd(fd).into() std::net::UdpSocket::from_raw_fd(fd).into()
} }
} }

Loading…
Cancel
Save