1 use core::pin::Pin;
2 use futures_core::future::{Future, TryFuture};
3 use futures_core::task::{Context, Poll};
4 use crate::future::Either;
5 use crate::try_future::TryFutureExt;
6 
7 /// Future for the [`try_select()`] function.
8 #[must_use = "futures do nothing unless you `.await` or poll them"]
9 #[derive(Debug)]
10 pub struct TrySelect<A: Unpin, B: Unpin> {
11     inner: Option<(A, B)>,
12 }
13 
14 impl<A: Unpin, B: Unpin> Unpin for TrySelect<A, B> {}
15 
16 /// Waits for either one of two differently-typed futures to complete.
17 ///
18 /// This function will return a new future which awaits for either one of both
19 /// futures to complete. The returned future will finish with both the value
20 /// resolved and a future representing the completion of the other work.
21 ///
22 /// Note that this function consumes the receiving futures and returns a
23 /// wrapped version of them.
24 ///
25 /// Also note that if both this and the second future have the same
26 /// success/error type you can use the `Either::factor_first` method to
27 /// conveniently extract out the value at the end.
28 ///
29 /// # Examples
30 ///
31 /// ```
32 /// use futures::future::{self, Either, Future, FutureExt, TryFuture, TryFutureExt};
33 ///
34 /// // A poor-man's try_join implemented on top of select
35 ///
36 /// fn try_join<A, B, E>(a: A, b: B) -> impl TryFuture<Ok=(A::Ok, B::Ok), Error=E>
37 ///      where A: TryFuture<Error = E> + Unpin + 'static,
38 ///            B: TryFuture<Error = E> + Unpin + 'static,
39 ///            E: 'static,
40 /// {
41 ///     future::try_select(a, b).then(|res| -> Box<dyn Future<Output = Result<_, _>> + Unpin> {
42 ///         match res {
43 ///             Ok(Either::Left((x, b))) => Box::new(b.map_ok(move |y| (x, y))),
44 ///             Ok(Either::Right((y, a))) => Box::new(a.map_ok(move |x| (x, y))),
45 ///             Err(Either::Left((e, _))) => Box::new(future::err(e)),
46 ///             Err(Either::Right((e, _))) => Box::new(future::err(e)),
47 ///         }
48 ///     })
49 /// }
50 /// ```
try_select<A, B>(future1: A, future2: B) -> TrySelect<A, B> where A: TryFuture + Unpin, B: TryFuture + Unpin51 pub fn try_select<A, B>(future1: A, future2: B) -> TrySelect<A, B>
52     where A: TryFuture + Unpin, B: TryFuture + Unpin
53 {
54     TrySelect { inner: Some((future1, future2)) }
55 }
56 
57 impl<A: Unpin, B: Unpin> Future for TrySelect<A, B>
58     where A: TryFuture, B: TryFuture
59 {
60     #[allow(clippy::type_complexity)]
61     type Output = Result<
62         Either<(A::Ok, B), (B::Ok, A)>,
63         Either<(A::Error, B), (B::Error, A)>,
64     >;
65 
poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>66     fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
67         let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
68         match a.try_poll_unpin(cx) {
69             Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Left((x, b)))),
70             Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Left((x, b)))),
71             Poll::Pending => match b.try_poll_unpin(cx) {
72                 Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Right((x, a)))),
73                 Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Right((x, a)))),
74                 Poll::Pending => {
75                     self.inner = Some((a, b));
76                     Poll::Pending
77                 }
78             }
79         }
80     }
81 }
82