From 79073d017bac0b5e1bf80de7194d6b82ee044827 Mon Sep 17 00:00:00 2001 From: Sherlock Holo Date: Tue, 26 May 2020 00:13:28 +0800 Subject: [PATCH 1/2] feat: add readable() and writable() async functions add these async functions should can solve problem in #139 --- src/async_io.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/async_io.rs b/src/async_io.rs index eec7dd3..429a2d1 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -159,6 +159,7 @@ impl IntoRawFd for Async { self.into_inner().unwrap().into_raw_fd() } } + #[cfg(windows)] impl Async { /// Creates an async I/O handle. @@ -454,6 +455,54 @@ impl Async { self.source.writable().await?; } } + + /// Wait until the async I/O handle is readable. + /// + /// # Examples + /// + /// ``` + /// use smol::Async; + /// use std::net::{TcpListener, TcpStream}; + /// use futures_util::io::AsyncWriteExt; + /// + /// # smol::run(async { + /// let listener = Async::::bind("0.0.0.0:0")?; + /// let addr = listener.get_ref().local_addr()?; + /// + /// let mut stream1 = Async::::connect(addr).await?; + /// let (stream2, _) = listener.accept().await?; + /// + /// stream1.write(b"test").await?; + /// stream2.readable().await?; + /// + /// # std::io::Result::Ok(()) }); + /// ``` + #[inline] + pub async fn readable(&self) -> io::Result<()> { + self.source.readable().await + } + + /// Wait until the async I/O handle is writable. + /// + /// # Examples + /// + /// ``` + /// use smol::Async; + /// use std::net::{TcpListener, TcpStream}; + /// + /// # smol::run(async { + /// let listener = Async::::bind("0.0.0.0:0")?; + /// let addr = listener.get_ref().local_addr()?; + /// + /// let stream = Async::::connect(addr).await?; + /// + /// stream.writable().await?; + /// # std::io::Result::Ok(()) }); + /// ``` + #[inline] + pub async fn writable(&self) -> io::Result<()> { + self.source.writable().await + } } impl Drop for Async { From b03b39b210d748fc85fadb5b028b977f7502aad8 Mon Sep 17 00:00:00 2001 From: Sherlock Holo Date: Tue, 26 May 2020 00:17:02 +0800 Subject: [PATCH 2/2] test: ensure doc test will run successfully bind 127.0.0.1:80 may fail when other program bind it before, use 0.0.0.0:0 can let kernel find a usable address --- src/async_io.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/async_io.rs b/src/async_io.rs index 429a2d1..9db53aa 100644 --- a/src/async_io.rs +++ b/src/async_io.rs @@ -191,7 +191,7 @@ impl Async { /// use std::net::TcpListener; /// /// # smol::run(async { - /// let listener = TcpListener::bind("127.0.0.1:80")?; + /// let listener = TcpListener::bind("0.0.0.0:0")?; /// let listener = Async::new(listener)?; /// # std::io::Result::Ok(()) }); /// ``` @@ -232,7 +232,7 @@ impl Async { /// use std::net::TcpListener; /// /// # smol::run(async { - /// let listener = Async::::bind("127.0.0.1:80")?; + /// let listener = Async::::bind("0.0.0.0:0")?; /// let inner = listener.get_ref(); /// # std::io::Result::Ok(()) }); /// ``` @@ -249,7 +249,7 @@ impl Async { /// use std::net::TcpListener; /// /// # smol::run(async { - /// let mut listener = Async::::bind("127.0.0.1:80")?; + /// let mut listener = Async::::bind("0.0.0.0:0")?; /// let inner = listener.get_mut(); /// # std::io::Result::Ok(()) }); /// ``` @@ -266,7 +266,7 @@ impl Async { /// use std::net::TcpListener; /// /// # smol::run(async { - /// let listener = Async::::bind("127.0.0.1:80")?; + /// let listener = Async::::bind("0.0.0.0:0")?; /// let inner = listener.into_inner()?; /// # std::io::Result::Ok(()) }); /// ```