daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Oct 12th, 2018 46 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use tokio::prelude::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
  2.  
  3. const BUFF_SIZE: usize = 2048;
  4.  
  5. pub async fn copy<R, W>(mut reader: R, mut writer: W) -> usize
  6. where
  7.     R: AsyncRead,
  8.     W: AsyncWrite,
  9. {
  10.     let mut buff: Box<[u8]> = vec![0; BUFF_SIZE].into_boxed_slice();
  11.     let mut totall: usize = 0;
  12.  
  13.     while let Ok(n) = await!(reader.read_async(&mut buff)) {
  14.         // Nothing more to read
  15.         if n == 0 { break }
  16.        
  17.         match await!(writer.write_async(&mut buff[..n])) {
  18.            
  19.             // empty write
  20.             Ok(0) => return totall,
  21.            
  22.             // normal write
  23.             Ok(n) => continue,
  24.  
  25.             // error
  26.             Err(e) => return totall
  27.         }
  28.     }
  29.    
  30.     await!(writer.flush_async());
  31.    
  32.     totall
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top