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