From a5a00d7b1465b728592ae0cff55fbefba853d528 Mon Sep 17 00:00:00 2001 From: k-nasa Date: Thu, 17 Oct 2019 16:29:23 +0900 Subject: [PATCH] feat: Add StdinLock struct --- src/io/stdin.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/io/stdin.rs b/src/io/stdin.rs index f6c4a25e..b4ccffbc 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -47,6 +47,11 @@ pub fn stdin() -> Stdin { #[derive(Debug)] pub struct Stdin(Mutex); +#[derive(Debug)] +pub struct StdinLock<'a>(std::io::StdinLock<'a>); + +unsafe impl Send for StdinLock<'_> {} + /// The state of the asynchronous stdin. /// /// The stdin can be either idle or busy performing an asynchronous operation. @@ -157,12 +162,14 @@ impl Stdin { /// # /// # Ok(()) }) } /// ``` - pub async fn lock(&self) -> std::io::StdinLock<'static> { + pub async fn lock(&self) -> StdinLock<'static> { lazy_static! { static ref STDIN: std::io::Stdin = std::io::stdin(); } - STDIN.lock() + blocking::spawn(async { + StdinLock(STDIN.lock()) + }).await } } @@ -248,3 +255,13 @@ cfg_if! { } } } + +impl Read for StdinLock<'_> { + fn poll_read( + self: Pin<&mut Self>, + _cx: &mut Context<'_>, + _buf: &mut [u8], + ) -> Poll> { + unimplemented!() + } +}