1 | use assert_matches::assert_matches; |
2 | use futures::executor::block_on; |
3 | use futures::future::lazy; |
4 | use futures::io::{AsyncWrite, Cursor}; |
5 | use futures::task::Poll; |
6 | use std::pin::Pin; |
7 | |
8 | #[test] |
9 | fn cursor_asyncwrite_vec() { |
10 | let mut cursor = Cursor::new(vec![0; 5]); |
11 | block_on(lazy(|cx| { |
12 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[1, 2]), Poll::Ready(Ok(2))); |
13 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[3, 4]), Poll::Ready(Ok(2))); |
14 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[5, 6]), Poll::Ready(Ok(2))); |
15 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[6, 7]), Poll::Ready(Ok(2))); |
16 | })); |
17 | assert_eq!(cursor.into_inner(), [1, 2, 3, 4, 5, 6, 6, 7]); |
18 | } |
19 | |
20 | #[test] |
21 | fn cursor_asyncwrite_box() { |
22 | let mut cursor = Cursor::new(vec![0; 5].into_boxed_slice()); |
23 | block_on(lazy(|cx| { |
24 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[1, 2]), Poll::Ready(Ok(2))); |
25 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[3, 4]), Poll::Ready(Ok(2))); |
26 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[5, 6]), Poll::Ready(Ok(1))); |
27 | assert_matches!(Pin::new(&mut cursor).poll_write(cx, &[6, 7]), Poll::Ready(Ok(0))); |
28 | })); |
29 | assert_eq!(&*cursor.into_inner(), [1, 2, 3, 4, 5]); |
30 | } |
31 | |