Skip to content

Locking for stdin #334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 2, 2019
30 changes: 30 additions & 0 deletions src/io/stderr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use std::pin::Pin;
use std::sync::Mutex;

Expand Down Expand Up @@ -79,6 +80,35 @@ enum Operation {
Flush(io::Result<()>),
}

impl Stderr {
/// Locks this handle to the standard error stream, returning a writable guard.
///
/// The lock is released when the returned lock goes out of scope. The returned guard also implements the Write trait for writing data.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use std::io::Write;
///
/// let stderr = io::stderr();
/// let mut handle = stderr.lock().await;
///
/// handle.write_all(b"hello world")?;
/// #
/// # Ok(()) }) }
/// ```
pub async fn lock(&self) -> std::io::StderrLock<'static> {
lazy_static! {
static ref STDERR: std::io::Stderr = std::io::stderr();
}

STDERR.lock()
}
}

impl Write for Stderr {
fn poll_write(
mut self: Pin<&mut Self>,
Expand Down
47 changes: 47 additions & 0 deletions src/io/stdin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use std::pin::Pin;
use std::sync::Mutex;

Expand Down Expand Up @@ -46,6 +47,11 @@ pub fn stdin() -> Stdin {
#[derive(Debug)]
pub struct Stdin(Mutex<State>);

#[derive(Debug)]
pub struct StdinLock<'a>(std::io::StdinLock<'a>);

unsafe impl Send for StdinLock<'_> {}

/// The state of the asynchronous stdin.
///
/// The stdin can be either idle or busy performing an asynchronous operation.
Expand Down Expand Up @@ -134,6 +140,37 @@ impl Stdin {
})
.await
}

/// 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.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use crate::async_std::prelude::*;
///
/// let mut buffer = String::new();
///
/// let stdin = io::stdin();
/// let mut handle = stdin.lock().await;
///
/// handle.read_to_string(&mut buffer).await?;
/// #
/// # Ok(()) }) }
/// ```
pub async fn lock(&self) -> StdinLock<'static> {
lazy_static! {
static ref STDIN: std::io::Stdin = std::io::stdin();
}

blocking::spawn(async {
StdinLock(STDIN.lock())
}).await
}
}

impl Read for Stdin {
Expand Down Expand Up @@ -218,3 +255,13 @@ cfg_if! {
}
}
}

impl Read for StdinLock<'_> {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unimplemented!()
}
}
30 changes: 30 additions & 0 deletions src/io/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use std::pin::Pin;
use std::sync::Mutex;

Expand Down Expand Up @@ -79,6 +80,35 @@ enum Operation {
Flush(io::Result<()>),
}

impl Stdout {
/// Locks this handle to the standard error stream, returning a writable guard.
///
/// The lock is released when the returned lock goes out of scope. The returned guard also implements the Write trait for writing data.
///
/// # Examples
///
/// ```no_run
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
/// #
/// use async_std::io;
/// use std::io::Write;
///
/// let stdout = io::stdout();
/// let mut handle = stdout.lock().await;
///
/// handle.write_all(b"hello world")?;
/// #
/// # Ok(()) }) }
/// ```
pub async fn lock(&self) -> std::io::StdoutLock<'static> {
lazy_static! {
static ref STDOUT: std::io::Stdout = std::io::stdout();
}

STDOUT.lock()
}
}

impl Write for Stdout {
fn poll_write(
mut self: Pin<&mut Self>,
Expand Down