|
1 | 1 | cfg_unstable! {
|
2 | 2 | mod delay;
|
3 | 3 | mod flatten;
|
| 4 | + mod race; |
| 5 | + mod try_race; |
4 | 6 |
|
5 | 7 | use std::time::Duration;
|
6 | 8 |
|
7 | 9 | use delay::DelayFuture;
|
8 | 10 | use flatten::FlattenFuture;
|
9 | 11 | use crate::future::IntoFuture;
|
| 12 | + use race::Race; |
| 13 | + use try_race::TryRace; |
10 | 14 | }
|
11 | 15 |
|
12 | 16 | extension_trait! {
|
@@ -156,6 +160,94 @@ extension_trait! {
|
156 | 160 | {
|
157 | 161 | FlattenFuture::new(self)
|
158 | 162 | }
|
| 163 | + |
| 164 | + #[doc = r#" |
| 165 | + Waits for one of two similarly-typed futures to complete. |
| 166 | +
|
| 167 | + Awaits multiple futures simultaneously, returning the output of the |
| 168 | + first future that completes. |
| 169 | +
|
| 170 | + This function will return a new future which awaits for either one of both |
| 171 | + futures to complete. If multiple futures are completed at the same time, |
| 172 | + resolution will occur in the order that they have been passed. |
| 173 | +
|
| 174 | + Note that this macro consumes all futures passed, and once a future is |
| 175 | + completed, all other futures are dropped. |
| 176 | +
|
| 177 | + This macro is only usable inside of async functions, closures, and blocks. |
| 178 | +
|
| 179 | + # Examples |
| 180 | +
|
| 181 | + ``` |
| 182 | + # async_std::task::block_on(async { |
| 183 | + use async_std::prelude::*; |
| 184 | + use async_std::future; |
| 185 | +
|
| 186 | + let a = future::pending(); |
| 187 | + let b = future::ready(1u8); |
| 188 | + let c = future::ready(2u8); |
| 189 | +
|
| 190 | + let f = a.race(b).race(c); |
| 191 | + assert_eq!(f.await, 1u8); |
| 192 | + # }); |
| 193 | + ``` |
| 194 | + "#] |
| 195 | + #[cfg(any(feature = "unstable", feature = "docs"))] |
| 196 | + #[cfg_attr(feature = "docs", doc(cfg(unstable)))] |
| 197 | + fn race<F>( |
| 198 | + self, |
| 199 | + other: F |
| 200 | + ) -> impl Future<Output = <Self as std::future::Future>::Output> [Race<Self, F>] |
| 201 | + where |
| 202 | + Self: std::future::Future + Sized, |
| 203 | + F: std::future::Future<Output = <Self as std::future::Future>::Output>, |
| 204 | + { |
| 205 | + Race::new(self, other) |
| 206 | + } |
| 207 | + |
| 208 | + #[doc = r#" |
| 209 | + Waits for one of two similarly-typed fallible futures to complete. |
| 210 | +
|
| 211 | + Awaits multiple futures simultaneously, returning all results once complete. |
| 212 | +
|
| 213 | + `try_race` is similar to [`race`], but keeps going if a future |
| 214 | + resolved to an error until all futures have been resolved. In which case |
| 215 | + an error is returned. |
| 216 | +
|
| 217 | + The ordering of which value is yielded when two futures resolve |
| 218 | + simultaneously is intentionally left unspecified. |
| 219 | +
|
| 220 | + # Examples |
| 221 | +
|
| 222 | + ``` |
| 223 | + # fn main() -> std::io::Result<()> { async_std::task::block_on(async { |
| 224 | + # |
| 225 | + use async_std::prelude::*; |
| 226 | + use async_std::future; |
| 227 | + use std::io::{Error, ErrorKind}; |
| 228 | +
|
| 229 | + let a = future::pending::<Result<_, Error>>(); |
| 230 | + let b = future::ready(Err(Error::from(ErrorKind::Other))); |
| 231 | + let c = future::ready(Ok(1u8)); |
| 232 | +
|
| 233 | + let f = a.try_race(b).try_race(c); |
| 234 | + assert_eq!(f.await?, 1u8); |
| 235 | + # |
| 236 | + # Ok(()) }) } |
| 237 | + ``` |
| 238 | + "#] |
| 239 | + #[cfg(any(feature = "unstable", feature = "docs"))] |
| 240 | + #[cfg_attr(feature = "docs", doc(cfg(unstable)))] |
| 241 | + fn try_race<F: std::future::Future, T, E>( |
| 242 | + self, |
| 243 | + other: F |
| 244 | + ) -> impl Future<Output = <Self as std::future::Future>::Output> [TryRace<Self, F>] |
| 245 | + where |
| 246 | + Self: std::future::Future<Output = Result<T, E>> + Sized, |
| 247 | + F: std::future::Future<Output = <Self as std::future::Future>::Output>, |
| 248 | + { |
| 249 | + TryRace::new(self, other) |
| 250 | + } |
159 | 251 | }
|
160 | 252 |
|
161 | 253 | impl<F: Future + Unpin + ?Sized> Future for Box<F> {
|
|
0 commit comments