• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use futures::future;
2 use futures::stream;
3 
4 use futures_test::future::FutureTestExt;
5 use futures_test::{
6     assert_stream_done, assert_stream_next, assert_stream_pending,
7 };
8 
9 #[test]
unfold1()10 fn unfold1() {
11     let mut stream = stream::unfold(0, |state| {
12         if state <= 2 {
13             future::ready(Some((state * 2, state + 1))).pending_once()
14         } else {
15             future::ready(None).pending_once()
16         }
17     });
18 
19     // Creates the future with the closure
20     // Not ready (delayed future)
21     assert_stream_pending!(stream);
22     // Future is ready, yields the item
23     assert_stream_next!(stream, 0);
24 
25     // Repeat
26     assert_stream_pending!(stream);
27     assert_stream_next!(stream, 2);
28 
29     assert_stream_pending!(stream);
30     assert_stream_next!(stream, 4);
31 
32     // No more items
33     assert_stream_pending!(stream);
34     assert_stream_done!(stream);
35 }
36