mirror of https://github.com/stjepang/smol
feat: add readable() and writable() async functions
add these async functions should can solve problem in #139
This commit is contained in:
parent
2e2b8148ed
commit
79073d017b
|
@ -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> {
|
||||
|
|
Loading…
Reference in New Issue