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