Skip to content

Commit 94ef3dc

Browse files
committed
feat: Add Stdout::lock
1 parent 9b09806 commit 94ef3dc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/io/stdout.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use lazy_static::lazy_static;
12
use std::pin::Pin;
23
use std::sync::Mutex;
34

@@ -79,6 +80,35 @@ enum Operation {
7980
Flush(io::Result<()>),
8081
}
8182

83+
impl Stdout {
84+
/// Locks this handle to the standard error stream, returning a writable guard.
85+
///
86+
/// The lock is released when the returned lock goes out of scope. The returned guard also implements the Write trait for writing data.
87+
///
88+
/// # Examples
89+
///
90+
/// ```no_run
91+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
92+
/// #
93+
/// use async_std::io;
94+
/// use std::io::Write;
95+
///
96+
/// let stdout = io::stdout();
97+
/// let mut handle = stdout.lock().await;
98+
///
99+
/// handle.write_all(b"hello world")?;
100+
/// #
101+
/// # Ok(()) }) }
102+
/// ```
103+
pub async fn lock(&self) -> std::io::StdoutLock<'static> {
104+
lazy_static! {
105+
static ref STDOUT: std::io::Stdout = std::io::stdout();
106+
}
107+
108+
STDOUT.lock()
109+
}
110+
}
111+
82112
impl Write for Stdout {
83113
fn poll_write(
84114
mut self: Pin<&mut Self>,

0 commit comments

Comments
 (0)