1 #![warn(rust_2018_idioms)] 2 #![cfg(feature = "full")] 3 4 use tokio::io::{self, AsyncRead, ReadBuf}; 5 use tokio_test::assert_ok; 6 7 use std::pin::Pin; 8 use std::task::{Context, Poll}; 9 10 #[tokio::test] copy()11async fn copy() { 12 struct Rd(bool); 13 14 impl AsyncRead for Rd { 15 fn poll_read( 16 mut self: Pin<&mut Self>, 17 _cx: &mut Context<'_>, 18 buf: &mut ReadBuf<'_>, 19 ) -> Poll<io::Result<()>> { 20 if self.0 { 21 buf.put_slice(b"hello world"); 22 self.0 = false; 23 Poll::Ready(Ok(())) 24 } else { 25 Poll::Ready(Ok(())) 26 } 27 } 28 } 29 30 let mut rd = Rd(true); 31 let mut wr = Vec::new(); 32 33 let n = assert_ok!(io::copy(&mut rd, &mut wr).await); 34 assert_eq!(n, 11); 35 assert_eq!(wr, b"hello world"); 36 } 37