From 94ef3dc2b2cf0f32155e9643efd3a98c270a10f4 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 15 Oct 2019 21:44:23 +0900 Subject: [PATCH] feat: Add Stdout::lock --- src/io/stdout.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/io/stdout.rs b/src/io/stdout.rs index 7849f1ce..31801381 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -1,3 +1,4 @@ +use lazy_static::lazy_static; use std::pin::Pin; use std::sync::Mutex; @@ -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>,