From: Ian Jackson Date: Tue, 1 Jun 2021 12:54:07 +0000 (+0100) Subject: utils: Provide io_copy_interactive X-Git-Tag: otter-0.7.0~155 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=e60b286438c7f0f8f9493959e3c38720a4a96da8;p=otter.git utils: Provide io_copy_interactive stdlib io::copy insists on buffering. Signed-off-by: Ian Jackson --- diff --git a/src/utils.rs b/src/utils.rs index 9c73675a..4b7c0704 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -676,6 +676,24 @@ impl anyhow::Error { } } +#[throws(Either)] +pub fn io_copy_interactive(read: &mut BufReader, write: &mut W) +where R: Read, W: Write { + loop { + let buf = read.fill_buf().map_err(Either::Left)?; + if buf.len() == 0 { break } + + let did = (||{ + let did = write.write(buf)?; + if did == 0 { throw!(ErrorKind::WriteZero) } + Ok::<_,io::Error>(did) + })().map_err(Either::Right)?; + + read.consume(did); + write.flush().map_err(Either::Right)?; + } +} + #[throws(fmt::Error)] pub fn fmt_hex(f: &mut Formatter, buf: &[u8]) { for v in buf { write!(f, "{:02x}", v)?; }