async_std/future/future/
try_race.rs1use std::pin::Pin;
2
3use crate::future::MaybeDone;
4use pin_project_lite::pin_project;
5
6use crate::task::{Context, Poll};
7use std::future::Future;
8
9pin_project! {
10 #[allow(missing_docs)]
11 #[allow(missing_debug_implementations)]
12 pub struct TryRace<L, R>
13 where
14 L: Future,
15 R: Future<Output = L::Output>
16 {
17 #[pin] left: MaybeDone<L>,
18 #[pin] right: MaybeDone<R>,
19 }
20}
21
22impl<L, R> TryRace<L, R>
23where
24 L: Future,
25 R: Future<Output = L::Output>,
26{
27 pub(crate) fn new(left: L, right: R) -> Self {
28 Self {
29 left: MaybeDone::new(left),
30 right: MaybeDone::new(right),
31 }
32 }
33}
34
35impl<L, R, T, E> Future for TryRace<L, R>
36where
37 L: Future<Output = Result<T, E>>,
38 R: Future<Output = L::Output>,
39{
40 type Output = L::Output;
41
42 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
43 let this = self.project();
44 let mut left_errored = false;
45
46 let mut left = this.left;
48 if Future::poll(Pin::new(&mut left), cx).is_ready() {
49 if left.as_ref().output().unwrap().is_ok() {
50 return Poll::Ready(left.take().unwrap());
51 } else {
52 left_errored = true;
53 }
54 }
55
56 let mut right = this.right;
59 let is_ready = Future::poll(Pin::new(&mut right), cx).is_ready();
60 if is_ready && (right.as_ref().output().unwrap().is_ok() || left_errored) {
61 return Poll::Ready(right.take().unwrap());
62 }
63
64 Poll::Pending
65 }
66}