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