• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #[test]
read_to_string()2 fn read_to_string() {
3     use futures::executor::block_on;
4     use futures::io::{AsyncReadExt, Cursor};
5 
6     let mut c = Cursor::new(&b""[..]);
7     let mut v = String::new();
8     assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 0);
9     assert_eq!(v, "");
10 
11     let mut c = Cursor::new(&b"1"[..]);
12     let mut v = String::new();
13     assert_eq!(block_on(c.read_to_string(&mut v)).unwrap(), 1);
14     assert_eq!(v, "1");
15 
16     let mut c = Cursor::new(&b"\xff"[..]);
17     let mut v = String::new();
18     assert!(block_on(c.read_to_string(&mut v)).is_err());
19 }
20 
21 #[test]
interleave_pending()22 fn interleave_pending() {
23     use futures::future::Future;
24     use futures::stream::{self, StreamExt, TryStreamExt};
25     use futures::io::AsyncReadExt;
26     use futures_test::io::AsyncReadTestExt;
27 
28     fn run<F: Future + Unpin>(mut f: F) -> F::Output {
29         use futures::future::FutureExt;
30         use futures_test::task::noop_context;
31         use futures::task::Poll;
32 
33         let mut cx = noop_context();
34         loop {
35             if let Poll::Ready(x) = f.poll_unpin(&mut cx) {
36                 return x;
37             }
38         }
39     }
40     let mut buf = stream::iter(vec![&b"12"[..], &b"33"[..], &b"3"[..]])
41         .map(Ok)
42         .into_async_read()
43         .interleave_pending();
44 
45     let mut v = String::new();
46     assert_eq!(run(buf.read_to_string(&mut v)).unwrap(), 5);
47     assert_eq!(v, "12333");
48 }
49