2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-01-19 20:13:51 +00:00

feat: Add Stdout::lock

This commit is contained in:
k-nasa 2019-10-15 21:44:23 +09:00
parent 9b09806593
commit 94ef3dc2b2

View file

@ -1,3 +1,4 @@
use lazy_static::lazy_static;
use std::pin::Pin; use std::pin::Pin;
use std::sync::Mutex; use std::sync::Mutex;
@ -79,6 +80,35 @@ enum Operation {
Flush(io::Result<()>), 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 { impl Write for Stdout {
fn poll_write( fn poll_write(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,