diff --git a/src/io/mod.rs b/src/io/mod.rs index 9a125b20..c81d82f9 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -282,9 +282,9 @@ pub use read::Read; pub use repeat::{repeat, Repeat}; pub use seek::Seek; pub use sink::{sink, Sink}; -pub use stderr::{stderr, Stderr}; -pub use stdin::{stdin, Stdin}; -pub use stdout::{stdout, Stdout}; +pub use stderr::{stderr, Stderr, StderrLock}; +pub use stdin::{stdin, Stdin, StdinLock}; +pub use stdout::{stdout, Stdout, StdoutLock}; pub use timeout::timeout; pub use write::Write; diff --git a/src/io/stderr.rs b/src/io/stderr.rs index 7cd95aa5..4e727f21 100644 --- a/src/io/stderr.rs +++ b/src/io/stderr.rs @@ -1,4 +1,5 @@ use lazy_static::lazy_static; +use std::io::Write as StdWrite; use std::pin::Pin; use std::sync::Mutex; @@ -54,6 +55,11 @@ pub fn stderr() -> Stderr { #[derive(Debug)] pub struct Stderr(Mutex); +/// A locked reference to the Stderr handle. +/// This handle implements the [`Write`] traits, and is constructed via the [`Stderr::lock`] method. +/// +/// [`Write`]: trait.Read.html +/// [`Stderr::lock`]: struct.Stderr.html#method.lock #[derive(Debug)] pub struct StderrLock<'a>(std::io::StderrLock<'a>); @@ -104,12 +110,12 @@ impl Stderr { /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::io; - /// use std::io::Write; + /// use async_std::prelude::*; /// /// let stderr = io::stderr(); /// let mut handle = stderr.lock().await; /// - /// handle.write_all(b"hello world")?; + /// handle.write_all(b"hello world").await?; /// # /// # Ok(()) }) } /// ``` @@ -227,18 +233,18 @@ cfg_windows! { impl Write for StderrLock<'_> { fn poll_write( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, _cx: &mut Context<'_>, - _buf: &[u8], + buf: &[u8], ) -> Poll> { - unimplemented!() + Poll::Ready(self.0.write(buf)) } - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - unimplemented!() + fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(self.0.flush()) } - fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - unimplemented!() + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.poll_flush(cx) } } diff --git a/src/io/stdin.rs b/src/io/stdin.rs index 82a0b00b..9fb28bab 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -55,6 +55,11 @@ pub fn stdin() -> Stdin { #[derive(Debug)] pub struct Stdin(Mutex); +/// A locked reference to the Stdin handle. +/// This handle implements the [`Read`] traits, and is constructed via the [`Stdin::lock`] method. +/// +/// [`Read`]: trait.Read.html +/// [`Stdin::lock`]: struct.Stdin.html#method.lock #[derive(Debug)] pub struct StdinLock<'a>(std::io::StdinLock<'a>); @@ -151,7 +156,7 @@ impl Stdin { /// Locks this handle to the standard input stream, returning a readable guard. /// - /// The lock is released when the returned lock goes out of scope. The returned guard also implements the Read and BufRead traits for accessing the underlying data. + /// The lock is released when the returned lock goes out of scope. The returned guard also implements the Read trait for accessing the underlying data. /// /// # Examples /// @@ -251,10 +256,12 @@ cfg_windows! { impl Read for StdinLock<'_> { fn poll_read( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, _cx: &mut Context<'_>, - _buf: &mut [u8], + buf: &mut [u8], ) -> Poll> { - unimplemented!() + use std::io::Read as StdRead; + + Poll::Ready(self.0.read(buf)) } } diff --git a/src/io/stdout.rs b/src/io/stdout.rs index 8d4ba273..c314837b 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -1,4 +1,5 @@ use lazy_static::lazy_static; +use std::io::Write as StdWrite; use std::pin::Pin; use std::sync::Mutex; @@ -54,6 +55,11 @@ pub fn stdout() -> Stdout { #[derive(Debug)] pub struct Stdout(Mutex); +/// A locked reference to the Stderr handle. +/// This handle implements the [`Write`] traits, and is constructed via the [`Stdout::lock`] method. +/// +/// [`Write`]: trait.Read.html +/// [`Stdout::lock`]: struct.Stdout.html#method.lock #[derive(Debug)] pub struct StdoutLock<'a>(std::io::StdoutLock<'a>); @@ -104,12 +110,12 @@ impl Stdout { /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { /// # /// use async_std::io; - /// use std::io::Write; + /// use async_std::prelude::*; /// /// let stdout = io::stdout(); /// let mut handle = stdout.lock().await; /// - /// handle.write_all(b"hello world")?; + /// handle.write_all(b"hello world").await?; /// # /// # Ok(()) }) } /// ``` @@ -227,18 +233,18 @@ cfg_windows! { impl Write for StdoutLock<'_> { fn poll_write( - self: Pin<&mut Self>, + mut self: Pin<&mut Self>, _cx: &mut Context<'_>, - _buf: &[u8], + buf: &[u8], ) -> Poll> { - unimplemented!() + Poll::Ready(self.0.write(buf)) } - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - unimplemented!() + fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { + Poll::Ready(self.0.flush()) } - fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { - unimplemented!() + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { + self.poll_flush(cx) } }