-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3de11b
commit 9a5eacf
Showing
3 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "example_0003_copy_reader_to_writer" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use std::io::{Read, Write, self, ErrorKind}; | ||
|
||
const DEFAULT_BUFFER_SIZE: usize = 8 * 1024; | ||
|
||
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64> | ||
where R: Read, W: Write | ||
{ | ||
let mut buf = [0; DEFAULT_BUFFER_SIZE]; | ||
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() == ErrorKind::Interrupted => continue, | ||
Err(e) => return Err(e), | ||
}; | ||
writer.write_all(&buf[..len])?; | ||
written += len as u64; | ||
} | ||
} |