forked from mirror/async-std
feat: Add Stdout::lock
This commit is contained in:
parent
9b09806593
commit
94ef3dc2b2
1 changed files with 30 additions and 0 deletions
|
@ -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>,
|
||||
|
|
Loading…
Reference in a new issue