Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created December 25, 2018 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitsuhiko/400dccbbd515f5de6052f13694717f46 to your computer and use it in GitHub Desktop.
Save mitsuhiko/400dccbbd515f5de6052f13694717f46 to your computer and use it in GitHub Desktop.
pub fn copy_with_progress<R: ?Sized, W: ?Sized>(
progress: &ProgressBar,
reader: &mut R,
writer: &mut W,
) -> io::Result<u64>
where
R: Read,
W: Write,
{
let mut buf = [0; 262144];
let mut written = 0;
loop {
let len = match reader.read(&mut buf) {
Ok(0) => return Ok(written),
Ok(len) => len,
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue,
Err(e) => return Err(e),
};
writer.write_all(&buf[..len])?;
written += len as u64;
progress.inc(len as u64);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment