From 28957d6256ac5d568eaed2555d568b5b7c047231 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 11 Mar 2021 14:27:42 -0500 Subject: only initialize blocking::Unblock once otherwise the background thread the first call is using will still be blocking on a second read, whose results will be thrown away before the new instance has a chance to read --- src/input.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/input.rs b/src/input.rs index 159e61d..a324c85 100644 --- a/src/input.rs +++ b/src/input.rs @@ -61,6 +61,8 @@ impl Drop for RawGuard { } pub struct Input { + stdin: blocking::Unblock, + buf: Vec, pos: usize, @@ -79,6 +81,7 @@ impl Input { pub fn new_without_raw() -> Self { Self { + stdin: blocking::Unblock::new(std::io::stdin()), buf: Vec::with_capacity(4096), pos: 0, parse_utf8: true, @@ -414,7 +417,7 @@ impl Input { async fn fill_buf(&mut self) -> Result { self.buf.resize(4096, 0); self.pos = 0; - let bytes = read_stdin(&mut self.buf).await?; + let bytes = read_stdin(&mut self.stdin, &mut self.buf).await?; if bytes == 0 { return Ok(false); } @@ -423,9 +426,9 @@ impl Input { } } -async fn read_stdin(buf: &mut [u8]) -> Result { - blocking::Unblock::new(std::io::stdin()) - .read(buf) - .await - .map_err(Error::ReadStdin) +async fn read_stdin( + stdin: &mut blocking::Unblock, + buf: &mut [u8], +) -> Result { + stdin.read(buf).await.map_err(Error::ReadStdin) } -- cgit v1.2.3-54-g00ecf