|
| 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 | + |
| 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: Unpin, B: Unpin> { |
| 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 | +/// ``` |
| 50 | +pub fn try_select<A, B>(future1: A, future2: B) -> TrySelect<A, B> |
| 51 | + where A: TryFuture + Unpin, B: TryFuture + Unpin |
| 52 | +{ |
| 53 | + TrySelect { inner: Some((future1, future2)) } |
| 54 | +} |
| 55 | + |
| 56 | +impl<A: Unpin, B: Unpin> Future for TrySelect<A, B> |
| 57 | + where A: TryFuture, B: TryFuture |
| 58 | +{ |
| 59 | + #[allow(clippy::type_complexity)] |
| 60 | + type Output = Result< |
| 61 | + Either<(A::Ok, B), (B::Ok, A)>, |
| 62 | + Either<(A::Error, B), (B::Error, A)>, |
| 63 | + >; |
| 64 | + |
| 65 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 66 | + let (mut a, mut b) = self.inner.take().expect("cannot poll Select twice"); |
| 67 | + match Pin::new(&mut a).try_poll(cx) { |
| 68 | + Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Left((x, b)))), |
| 69 | + Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Left((x, b)))), |
| 70 | + Poll::Pending => match Pin::new(&mut b).try_poll(cx) { |
| 71 | + Poll::Ready(Err(x)) => Poll::Ready(Err(Either::Right((x, a)))), |
| 72 | + Poll::Ready(Ok(x)) => Poll::Ready(Ok(Either::Right((x, a)))), |
| 73 | + Poll::Pending => { |
| 74 | + self.inner = Some((a, b)); |
| 75 | + Poll::Pending |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments