use std::{ future::{Future, IntoFuture}, pin::Pin, task::Poll, }; use anyhow::Result; use futures::{ FutureExt, future::{JoinAll, join_all}, }; use pin_project_lite::pin_project; pin_project! { /// Future for the [JoinIterExt::join] method. pub struct Join where F: Future, { #[pin] inner: JoinAll, } } impl Future for Join where F: Future, { type Output = Vec; fn poll( self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll { self.project().inner.poll(cx) } } pub trait JoinIterExt: Iterator where F: Future, { /// Returns a future that resolves to a vector of the outputs of the futures /// in the iterator. fn join(self) -> Join; } pin_project! { /// Future for the [TryJoinIterExt::try_join] method. #[must_use] pub struct TryJoin where F: Future, { #[pin] inner: JoinAll, } } impl Future for TryJoin where F: Future>, { type Output = Result>; fn poll( self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, ) -> std::task::Poll { match self.project().inner.poll_unpin(cx) { std::task::Poll::Ready(res) => { std::task::Poll::Ready(res.into_iter().collect::>>()) } std::task::Poll::Pending => std::task::Poll::Pending, } } } pub trait TryJoinIterExt: Iterator where F: Future>, { /// Returns a future that resolves to a vector of the outputs of the futures /// in the iterator, or to an error if one of the futures fail. /// /// Unlike `Futures::future::try_join_all`, this returns the Error that /// occurs first in the list of futures, not the first to fail in time. fn try_join(self) -> TryJoin; } impl JoinIterExt for It where F: Future, IF: IntoFuture, It: Iterator, { fn join(self) -> Join { Join { inner: join_all(self.map(|f| f.into_future())), } } } impl TryJoinIterExt for It where F: Future>, IF: IntoFuture, IntoFuture = F>, It: Iterator, { fn try_join(self) -> TryJoin { TryJoin { inner: join_all(self.map(|f| f.into_future())), } } } pin_project! { /// Future for the [TryFlatJoinIterExt::try_flat_join] method. pub struct TryFlatJoin where F: Future, { #[pin] inner: JoinAll, } } impl Future for TryFlatJoin where F: Future>, I: IntoIterator, U: Iterator, { type Output = Result>; fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll { match self.project().inner.poll_unpin(cx) { Poll::Ready(res) => { let mut v = Vec::new(); for r in res { v.extend(r?); } Poll::Ready(Ok(v)) } Poll::Pending => Poll::Pending, } } } pub trait TryFlatJoinIterExt: Iterator where F: Future>, I: IntoIterator, U: Iterator, { /// Returns a future that resolves to a vector of the outputs of the futures /// in the iterator, or to an error if one of the futures fail. /// /// It also flattens the result. /// /// Unlike `Futures::future::try_join_all`, this returns the Error that /// occurs first in the list of futures, not the first to fail in time. fn try_flat_join(self) -> TryFlatJoin; } impl TryFlatJoinIterExt for It where F: Future>, IF: IntoFuture, IntoFuture = F>, It: Iterator, I: IntoIterator, U: Iterator, { fn try_flat_join(self) -> TryFlatJoin { TryFlatJoin { inner: join_all(self.map(|f| f.into_future())), } } }