diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-03-11 14:27:42 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-03-11 14:27:42 -0500 |
commit | 28957d6256ac5d568eaed2555d568b5b7c047231 (patch) | |
tree | 91c8a5d6c47f6ba4690dda9476dd7f2c4e864952 | |
parent | ae9f6d5c55280336bb130cc1a4bf712b45618eae (diff) | |
download | textmode-28957d6256ac5d568eaed2555d568b5b7c047231.tar.gz textmode-28957d6256ac5d568eaed2555d568b5b7c047231.zip |
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
-rw-r--r-- | src/input.rs | 15 |
1 files 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<std::io::Stdin>, + buf: Vec<u8>, 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<bool> { 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<usize> { - blocking::Unblock::new(std::io::stdin()) - .read(buf) - .await - .map_err(Error::ReadStdin) +async fn read_stdin( + stdin: &mut blocking::Unblock<std::io::Stdin>, + buf: &mut [u8], +) -> Result<usize> { + stdin.read(buf).await.map_err(Error::ReadStdin) } |