Skip to content

Commit f9741e7

Browse files
committed
feat: Add Stderr::lock
1 parent c7f6543 commit f9741e7

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/io/stderr.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 Stderr {
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 stderr = io::stderr();
97+
/// let mut handle = stderr.lock().await;
98+
///
99+
/// handle.write_all(b"hello world")?;
100+
/// #
101+
/// # Ok(()) }) }
102+
/// ```
103+
pub async fn lock(&self) -> std::io::StderrLock<'static> {
104+
lazy_static! {
105+
static ref STDERR: std::io::Stderr = std::io::stderr();
106+
}
107+
108+
STDERR.lock()
109+
}
110+
}
111+
82112
impl Write for Stderr {
83113
fn poll_write(
84114
mut self: Pin<&mut Self>,

0 commit comments

Comments
 (0)