Created
December 25, 2018 21:52
-
-
Save mitsuhiko/400dccbbd515f5de6052f13694717f46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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