Skip to content

Commit 7afb154

Browse files
ebkalderoncramertj
authored andcommitted
Add try_join_all tests, tweak formatting
1 parent b9dd6cf commit 7afb154

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

futures-util/src/try_future/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@ mod unwrap_or_else;
5555
pub use self::unwrap_or_else::UnwrapOrElse;
5656

5757
#[cfg(feature = "std")]
58-
mod join_all;
58+
mod try_join_all;
5959

6060
#[cfg(feature = "std")]
61-
pub use self::join_all::{join_all, JoinAll};
61+
pub use self::try_join_all::{try_join_all, TryJoinAll};
6262

6363
// Implementation details
6464
mod try_chain;

futures/tests/join_all.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use std::future::Future;
55
use futures::executor::block_on;
66
use std::fmt::Debug;
77

8-
fn assert_done<T: PartialEq + Debug, F: FnOnce() -> Box<dyn Future<Output=T> + Unpin>>(actual_fut: F, expected: T) {
8+
fn assert_done<T, F>(actual_fut: F, expected: T)
9+
where
10+
T: PartialEq + Debug,
11+
F: FnOnce() -> Box<dyn Future<Output = T> + Unpin>,
12+
{
913
let output = block_on(actual_fut());
10-
1114
assert_eq!(output, expected);
1215
}
1316

@@ -25,7 +28,7 @@ fn collect_collects() {
2528
fn join_all_iter_lifetime() {
2629
// In futures-rs version 0.1, this function would fail to typecheck due to an overly
2730
// conservative type parameterization of `JoinAll`.
28-
fn sizes<'a>(bufs: Vec<&'a [u8]>) -> Box<dyn Future<Output=Vec<usize>> + Unpin> {
31+
fn sizes<'a>(bufs: Vec<&'a [u8]>) -> Box<dyn Future<Output = Vec<usize>> + Unpin> {
2932
let iter = bufs.into_iter().map(|b| ready::<usize>(b.len()));
3033
Box::new(join_all(iter))
3134
}

futures/tests/try_join_all.rs

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

0 commit comments

Comments
 (0)