1 | #![warn (rust_2018_idioms)] |
2 | |
3 | use bytes::Bytes; |
4 | use tokio::io::AsyncReadExt; |
5 | use tokio_stream::iter; |
6 | use tokio_util::io::StreamReader; |
7 | |
8 | #[tokio::test ] |
9 | async fn test_stream_reader() -> std::io::Result<()> { |
10 | let stream = iter(vec![ |
11 | std::io::Result::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 = StreamReader::new(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 | |