From 9b0980659321f9b90bff21e435335d37de0434a4 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Tue, 15 Oct 2019 21:44:11 +0900 Subject: [PATCH] feat: Add Stdin::lock --- src/io/stdin.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/io/stdin.rs b/src/io/stdin.rs index 95a77b8..f6c4a25 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -1,3 +1,4 @@ +use lazy_static::lazy_static; use std::pin::Pin; use std::sync::Mutex; @@ -134,6 +135,35 @@ impl Stdin { }) .await } + + /// Locks this handle to the standard input stream, returning a readable guard. + /// + /// 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. + /// + /// # Examples + /// + /// ```no_run + /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # + /// use async_std::io; + /// use std::io::Read; + /// + /// let mut buffer = String::new(); + /// + /// let stdin = io::stdin(); + /// let mut handle = stdin.lock().await; + /// + /// handle.read_to_string(&mut buffer)?; + /// # + /// # Ok(()) }) } + /// ``` + pub async fn lock(&self) -> std::io::StdinLock<'static> { + lazy_static! { + static ref STDIN: std::io::Stdin = std::io::stdin(); + } + + STDIN.lock() + } } impl Read for Stdin {