• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use futures::executor::block_on;
2 use futures::pin_mut;
3 use futures_util::future::{err, ok, try_join_all, TryJoinAll};
4 use std::fmt::Debug;
5 use std::future::Future;
6 
7 #[track_caller]
assert_done<T>(actual_fut: impl Future<Output = T>, expected: T) where T: PartialEq + Debug,8 fn assert_done<T>(actual_fut: impl Future<Output = T>, expected: T)
9 where
10     T: PartialEq + Debug,
11 {
12     pin_mut!(actual_fut);
13     let output = block_on(actual_fut);
14     assert_eq!(output, expected);
15 }
16 
17 #[test]
collect_collects()18 fn collect_collects() {
19     assert_done(try_join_all(vec![ok(1), ok(2)]), Ok::<_, usize>(vec![1, 2]));
20     assert_done(try_join_all(vec![ok(1), err(2)]), Err(2));
21     assert_done(try_join_all(vec![ok(1)]), Ok::<_, usize>(vec![1]));
22     // REVIEW: should this be implemented?
23     // assert_done(try_join_all(Vec::<i32>::new()), Ok(vec![]));
24 
25     // TODO: needs more tests
26 }
27 
28 #[test]
try_join_all_iter_lifetime()29 fn try_join_all_iter_lifetime() {
30     // In futures-rs version 0.1, this function would fail to typecheck due to an overly
31     // conservative type parameterization of `TryJoinAll`.
32     fn sizes(bufs: Vec<&[u8]>) -> impl Future<Output = Result<Vec<usize>, ()>> {
33         let iter = bufs.into_iter().map(|b| ok::<usize, ()>(b.len()));
34         try_join_all(iter)
35     }
36 
37     assert_done(sizes(vec![&[1, 2, 3], &[], &[0]]), Ok(vec![3_usize, 0, 1]));
38 }
39 
40 #[test]
try_join_all_from_iter()41 fn try_join_all_from_iter() {
42     assert_done(
43         vec![ok(1), ok(2)].into_iter().collect::<TryJoinAll<_>>(),
44         Ok::<_, usize>(vec![1, 2]),
45     )
46 }
47