1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
5 use std
::task
::{Context, Poll}
;
6 use tokio
::io
::{AsyncRead, AsyncReadExt, ReadBuf}
;
7 use tokio_test
::assert_ok
;
10 async
fn read_to_end() {
12 let mut rd
: &[u8] = b
"hello world";
14 let n
= assert_ok
!(rd
.read_to_end(&mut buf
).await
);
16 assert_eq
!(buf
[..], b
"hello world"[..]);
19 #[derive(Copy, Clone, Debug)]
31 impl AsyncRead
for UninitTest
{
34 _cx
: &mut Context
<'_
>,
35 buf
: &mut ReadBuf
<'_
>,
36 ) -> Poll
<std
::io
::Result
<()>> {
37 let me
= Pin
::into_inner(self);
38 let real_num_init
= buf
.initialized().len() - buf
.filled().len();
39 assert_eq
!(real_num_init
, me
.num_init
, "{:?}", me
.state
);
42 State
::Initializing
=> {
43 buf
.initialize_unfilled_to(me
.num_init
+ 2);
47 if me
.num_init
== 24 {
48 me
.state
= State
::JustFilling
;
51 State
::JustFilling
=> {
55 if me
.num_init
== 15 {
56 // The buffer is resized on next call.
58 me
.state
= State
::Done
;
61 State
::Done
=> { /* .. do nothing .. */ }
69 async
fn read_to_end_uninit() {
70 let mut buf
= Vec
::with_capacity(64);
71 let mut test
= UninitTest
{
73 state
: State
::Initializing
,
76 test
.read_to_end(&mut buf
).await
.unwrap();
77 assert_eq
!(buf
.len(), 33);