futures_util/future/try_select.rs
1use crate::future::{Either, TryFutureExt};
2use core::pin::Pin;
3use futures_core::future::{Future, TryFuture};
4use futures_core::task::{Context, Poll};
5
6/// Future for the [`try_select()`] function.
7#[must_use = "futures do nothing unless you `.await` or poll them"]
8#[derive(Debug)]
9pub struct TrySelect<A, B> {
10 inner: Option<(A, B)>,
11}
12
13impl<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/// ```
50pub fn try_select<A, B>(future1: A, future2: B) -> TrySelect<A, B>
51where
52 A: TryFuture + Unpin,
53 B: TryFuture + Unpin,
54{
55 super::assert_future::<
56 Result<Either<(A::Ok, B), (B::Ok, A)>, Either<(A::Error, B), (B::Error, A)>>,
57 _,
58 >(TrySelect { inner: Some((future1, future2)) })
59}
60
61impl<A: Unpin, B: Unpin> Future for TrySelect<A, B>
62where
63 A: TryFuture,
64 B: TryFuture,
65{
66 #[allow(clippy::type_complexity)]
67 type Output = Result<Either<(A::Ok, B), (B::Ok, A)>, Either<(A::Error, B), (B::Error, A)>>;
68
69 fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
70 let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice");
71 match a.try_poll_unpin(cx) {
72 Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Left((x, b)))),
73 Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Left((x, b)))),
74 Poll::Pending => match b.try_poll_unpin(cx) {
75 Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Right((x, a)))),
76 Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Right((x, a)))),
77 Poll::Pending => {
78 self.inner = Some((a, b));
79 Poll::Pending
80 }
81 },
82 }
83 }
84}