feat: add readable() and writable() async functions

add these async functions should can solve problem in #139
This commit is contained in:
Sherlock Holo 2020-05-26 00:13:28 +08:00
parent 2e2b8148ed
commit 79073d017b
No known key found for this signature in database
GPG Key ID: 6BA34AF35741BD61
1 changed files with 49 additions and 0 deletions

View File

@ -159,6 +159,7 @@ impl<T: IntoRawFd> IntoRawFd for Async<T> {
self.into_inner().unwrap().into_raw_fd()
}
}
#[cfg(windows)]
impl<T: AsRawSocket> Async<T> {
/// Creates an async I/O handle.
@ -454,6 +455,54 @@ impl<T> Async<T> {
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::<TcpListener>::bind("0.0.0.0:0")?;
/// let addr = listener.get_ref().local_addr()?;
///
/// let mut stream1 = Async::<TcpStream>::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::<TcpListener>::bind("0.0.0.0:0")?;
/// let addr = listener.get_ref().local_addr()?;
///
/// let stream = Async::<TcpStream>::connect(addr).await?;
///
/// stream.writable().await?;
/// # std::io::Result::Ok(()) });
/// ```
#[inline]
pub async fn writable(&self) -> io::Result<()> {
self.source.writable().await
}
}
impl<T> Drop for Async<T> {