1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3 
4 use bytes::Bytes;
5 use tokio::io::{stream_reader, AsyncReadExt};
6 use tokio::stream::iter;
7 
8 #[tokio::test]
test_stream_reader() -> std::io::Result<()>9 async fn test_stream_reader() -> std::io::Result<()> {
10     let stream = iter(vec![
11         Ok(Bytes::from_static(&[])),
12         Ok(Bytes::from_static(&[0, 1, 2, 3])),
13         Ok(Bytes::from_static(&[])),
14         Ok(Bytes::from_static(&[4, 5, 6, 7])),
15         Ok(Bytes::from_static(&[])),
16         Ok(Bytes::from_static(&[8, 9, 10, 11])),
17         Ok(Bytes::from_static(&[])),
18     ]);
19 
20     let mut read = stream_reader(stream);
21 
22     let mut buf = [0; 5];
23     read.read_exact(&mut buf).await?;
24     assert_eq!(buf, [0, 1, 2, 3, 4]);
25 
26     assert_eq!(read.read(&mut buf).await?, 3);
27     assert_eq!(&buf[..3], [5, 6, 7]);
28 
29     assert_eq!(read.read(&mut buf).await?, 4);
30     assert_eq!(&buf[..4], [8, 9, 10, 11]);
31 
32     assert_eq!(read.read(&mut buf).await?, 0);
33 
34     Ok(())
35 }
36