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