1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
pub use UiFutureExt;
/// Wait for multiple futures to complete.
///
/// Join takes in many "subfutures" and return a single Future.
/// When awaited, the returned Future will drive all the subfutures
/// to completion and return all their results.
///
/// Subfutures may be passed in as either
/// * a tuple of up to 12 Futures (signature: `(F1, F2, ...) -> (F1::Output, F2::Output, ...)`)
/// * an array of Futures (signature: `[F; N] -> [F::Output; N]`)
/// * a [Vec] of Futures (signature: `Vec<F> -> Vec<F::Output>`)
///
/// ```rust
/// # use async_ui_web_core::combinators::join;
/// # let _ = async {
/// async fn do_something(input: i32) -> i32 {
/// // ...make a network request of something...
/// input * 2
/// }
/// // Join 2-tuple of Futures
/// let (res_1, res_2) = join((
/// do_something(21),
/// do_something(100)
/// )).await;
/// assert_eq!(res_1, 42);
/// assert_eq!(res_2, 200);
///
/// // Join array of Futures
/// let results: [i32; 20] = join(
/// core::array::from_fn(|idx| do_something(idx as i32))
/// ).await;
/// assert_eq!(
/// &results,
/// &*(0..20).map(|x| x * 2).collect::<Vec<_>>()
/// );
///
/// // Join vector of Futures
/// let results: Vec<i32> = join(
/// (0..100).map(|i| do_something(i as i32)).collect::<Vec<_>>()
/// ).await;
/// assert_eq!(
/// results,
/// (0..100).map(|x| x * 2).collect::<Vec<_>>()
/// );
/// # };
/// ```
/// Wait for the first future to complete.
///
/// Race takes in many "subfutures" and return a single Future.
/// When awaited, the returned Future will drive all of the subfuture until
/// any of them complete, and return the result of that completed subfuture.
///
/// Subfutures may be passed in as either
/// * a tuple of up to 12 Futures, all with the same output type
/// (signature: `(F1, F2, F3, ...) -> Output`)
/// * an array of Futures (signature: `[F; N] -> F::Output`)
/// * a [Vec] of Futures (signature: `Vec<F> -> F::Output`)
///
/// ```rust
/// # use async_ui_web_core::combinators::race;
/// # let _ = async {
/// async fn do_something(input: i32) -> i32 {
/// // ...make a network request of something...
/// input * 2
/// }
/// // Race 2-tuple of Futures
/// let result = race((
/// do_something(21),
/// do_something(100)
/// )).await;
/// // Don't know which one will win the race.
/// assert!(result == 42 || result == 200);
///
/// // Race array of Futures
/// let result: i32 = race(
/// core::array::from_fn::<_, 10, _>(|idx| do_something(idx as i32))
/// ).await;
///
/// // Race vector of Futures
/// let result: i32 = race(
/// (0..100).map(|i| do_something(i as i32)).collect::<Vec<_>>()
/// ).await;
/// # };
/// ```
/// Wait for all futures to complete successfully, or return early on error.
///
/// TryJoin takes in many fallible (returns [Result]) "subfutures" and return
/// a single Future.
/// When awaited, the returned Future will drive all of the subfuture until
/// either all of them return `Ok(_)` or any of them return `Err(_)`.
///
/// Subfutures may be passed in as either
/// * a tuple of up to 12 Futures, all with the same Error type
/// (signature: `(F1, F2, ...) -> Result<(F1::Ok, F2::Ok, ...), Error>`)
/// * an array of Futures
/// (signature: `[F; N] -> Result<[F::Ok; N], F::Error>`)
/// * a [Vec] of Futures
/// (signature: `Vec<F> -> Result<Vec<F::Ok>, F::Error>`)
/// Wait for any future to complete successfully.
///
/// RaceOk takes in many fallible (returns [Result]) "subfutures" and return
/// a single Future.
/// When awaited, the returned Future will drive all of the subfuture until
/// either one of them return `Ok(_)` or all of them return `Err(_)`.
///
/// Subfutures may be passed in as either
/// * a tuple of up to 12 Futures, all with the same Ok type
/// (signature: `(F1, F2, ...) -> Result<Ok, (F1::Error, F2::Error, ...)>`)
/// * an array of Futures
/// (signature: `[F; N] -> Result<F::Ok, [F::Error; N]>`)
/// * a [Vec] of Futures
/// (signature: `Vec<F> -> Result<F::Ok, Vec<F::Error>>`)