Skip to content

Commit 9b09806

Browse files
committed
feat: Add Stdin::lock
1 parent f9741e7 commit 9b09806

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/io/stdin.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

@@ -134,6 +135,35 @@ impl Stdin {
134135
})
135136
.await
136137
}
138+
139+
/// Locks this handle to the standard input stream, returning a readable guard.
140+
///
141+
/// The lock is released when the returned lock goes out of scope. The returned guard also implements the Read and BufRead traits for accessing the underlying data.
142+
///
143+
/// # Examples
144+
///
145+
/// ```no_run
146+
/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async {
147+
/// #
148+
/// use async_std::io;
149+
/// use std::io::Read;
150+
///
151+
/// let mut buffer = String::new();
152+
///
153+
/// let stdin = io::stdin();
154+
/// let mut handle = stdin.lock().await;
155+
///
156+
/// handle.read_to_string(&mut buffer)?;
157+
/// #
158+
/// # Ok(()) }) }
159+
/// ```
160+
pub async fn lock(&self) -> std::io::StdinLock<'static> {
161+
lazy_static! {
162+
static ref STDIN: std::io::Stdin = std::io::stdin();
163+
}
164+
165+
STDIN.lock()
166+
}
137167
}
138168

139169
impl Read for Stdin {

0 commit comments

Comments
 (0)