use core::pin::Pin; use futures_core::future::{FusedFuture, Future, TryFuture}; use futures_core::ready; use futures_core::task::{Context, Poll}; use pin_project_lite::pin_project; pin_project! { #[project = TryFlattenErrProj] #[derive(Debug)] pub enum TryFlattenErr { First { #[pin] f: Fut1 }, Second { #[pin] f: Fut2 }, Empty, } } impl TryFlattenErr { pub(crate) fn new(future: Fut1) -> Self { Self::First { f: future } } } impl FusedFuture for TryFlattenErr where Fut: TryFuture, Fut::Error: TryFuture, { fn is_terminated(&self) -> bool { match self { Self::Empty => true, _ => false, } } } impl Future for TryFlattenErr where Fut: TryFuture, Fut::Error: TryFuture, { type Output = Result::Error>; fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { Poll::Ready(loop { match self.as_mut().project() { TryFlattenErrProj::First { f } => match ready!(f.try_poll(cx)) { Err(f) => self.set(Self::Second { f }), Ok(e) => { self.set(Self::Empty); break Ok(e); } }, TryFlattenErrProj::Second { f } => { let output = ready!(f.try_poll(cx)); self.set(Self::Empty); break output; } TryFlattenErrProj::Empty => panic!("TryFlattenErr polled after completion"), } }) } }