Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use tokio::prelude::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
- const BUFF_SIZE: usize = 2048;
- pub async fn copy<R, W>(mut reader: R, mut writer: W) -> usize
- where
- R: AsyncRead,
- W: AsyncWrite,
- {
- let mut buff: Box<[u8]> = vec![0; BUFF_SIZE].into_boxed_slice();
- let mut totall: usize = 0;
- while let Ok(n) = await!(reader.read_async(&mut buff)) {
- // Nothing more to read
- if n == 0 { break }
- match await!(writer.write_async(&mut buff[..n])) {
- // empty write
- Ok(0) => return totall,
- // normal write
- Ok(n) => continue,
- // error
- Err(e) => return totall
- }
- }
- await!(writer.flush_async());
- totall
- }
Add Comment
Please, Sign In to add comment