mirror of
https://github.com/async-rs/async-std.git
synced 2025-01-19 20:13:51 +00:00
feat: Add Read and Write trait to Lock struct
This commit is contained in:
parent
35cb11e398
commit
2c91b30ee8
4 changed files with 44 additions and 25 deletions
|
@ -282,9 +282,9 @@ pub use read::Read;
|
||||||
pub use repeat::{repeat, Repeat};
|
pub use repeat::{repeat, Repeat};
|
||||||
pub use seek::Seek;
|
pub use seek::Seek;
|
||||||
pub use sink::{sink, Sink};
|
pub use sink::{sink, Sink};
|
||||||
pub use stderr::{stderr, Stderr};
|
pub use stderr::{stderr, Stderr, StderrLock};
|
||||||
pub use stdin::{stdin, Stdin};
|
pub use stdin::{stdin, Stdin, StdinLock};
|
||||||
pub use stdout::{stdout, Stdout};
|
pub use stdout::{stdout, Stdout, StdoutLock};
|
||||||
pub use timeout::timeout;
|
pub use timeout::timeout;
|
||||||
pub use write::Write;
|
pub use write::Write;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use std::io::Write as StdWrite;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
@ -54,6 +55,11 @@ pub fn stderr() -> Stderr {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Stderr(Mutex<State>);
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct StderrLock<'a>(std::io::StderrLock<'a>);
|
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 {
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
/// #
|
/// #
|
||||||
/// use async_std::io;
|
/// use async_std::io;
|
||||||
/// use std::io::Write;
|
/// use async_std::prelude::*;
|
||||||
///
|
///
|
||||||
/// let stderr = io::stderr();
|
/// let stderr = io::stderr();
|
||||||
/// let mut handle = stderr.lock().await;
|
/// let mut handle = stderr.lock().await;
|
||||||
///
|
///
|
||||||
/// handle.write_all(b"hello world")?;
|
/// handle.write_all(b"hello world").await?;
|
||||||
/// #
|
/// #
|
||||||
/// # Ok(()) }) }
|
/// # Ok(()) }) }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -227,18 +233,18 @@ cfg_windows! {
|
||||||
|
|
||||||
impl Write for StderrLock<'_> {
|
impl Write for StderrLock<'_> {
|
||||||
fn poll_write(
|
fn poll_write(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
_cx: &mut Context<'_>,
|
||||||
_buf: &[u8],
|
buf: &[u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
unimplemented!()
|
Poll::Ready(self.0.write(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
unimplemented!()
|
Poll::Ready(self.0.flush())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
unimplemented!()
|
self.poll_flush(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,6 +55,11 @@ pub fn stdin() -> Stdin {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Stdin(Mutex<State>);
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct StdinLock<'a>(std::io::StdinLock<'a>);
|
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.
|
/// 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
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -251,10 +256,12 @@ cfg_windows! {
|
||||||
|
|
||||||
impl Read for StdinLock<'_> {
|
impl Read for StdinLock<'_> {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
_cx: &mut Context<'_>,
|
||||||
_buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> 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 lazy_static::lazy_static;
|
||||||
|
use std::io::Write as StdWrite;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
|
|
||||||
|
@ -54,6 +55,11 @@ pub fn stdout() -> Stdout {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Stdout(Mutex<State>);
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct StdoutLock<'a>(std::io::StdoutLock<'a>);
|
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 {
|
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
|
||||||
/// #
|
/// #
|
||||||
/// use async_std::io;
|
/// use async_std::io;
|
||||||
/// use std::io::Write;
|
/// use async_std::prelude::*;
|
||||||
///
|
///
|
||||||
/// let stdout = io::stdout();
|
/// let stdout = io::stdout();
|
||||||
/// let mut handle = stdout.lock().await;
|
/// let mut handle = stdout.lock().await;
|
||||||
///
|
///
|
||||||
/// handle.write_all(b"hello world")?;
|
/// handle.write_all(b"hello world").await?;
|
||||||
/// #
|
/// #
|
||||||
/// # Ok(()) }) }
|
/// # Ok(()) }) }
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -227,18 +233,18 @@ cfg_windows! {
|
||||||
|
|
||||||
impl Write for StdoutLock<'_> {
|
impl Write for StdoutLock<'_> {
|
||||||
fn poll_write(
|
fn poll_write(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
_cx: &mut Context<'_>,
|
||||||
_buf: &[u8],
|
buf: &[u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
unimplemented!()
|
Poll::Ready(self.0.write(buf))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_flush(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
unimplemented!()
|
Poll::Ready(self.0.flush())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||||||
unimplemented!()
|
self.poll_flush(cx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue