feat: Add Read and Write trait to Lock struct

pull/334/head
k-nasa 5 years ago
parent 35cb11e398
commit 2c91b30ee8

@ -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;

@ -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<State>);
/// 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<io::Result<usize>> {
unimplemented!()
Poll::Ready(self.0.write(buf))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(self.0.flush())
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.poll_flush(cx)
}
}

@ -55,6 +55,11 @@ pub fn stdin() -> Stdin {
#[derive(Debug)]
pub struct Stdin(Mutex<State>);
/// 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<io::Result<usize>> {
unimplemented!()
use std::io::Read as StdRead;
Poll::Ready(self.0.read(buf))
}
}

@ -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<State>);
/// 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<io::Result<usize>> {
unimplemented!()
Poll::Ready(self.0.write(buf))
}
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
Poll::Ready(self.0.flush())
}
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unimplemented!()
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
self.poll_flush(cx)
}
}

Loading…
Cancel
Save