File size: 4,378 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
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<F>
where
F: Future,
{
#[pin]
inner: JoinAll<F>,
}
}
impl<T, F> Future for Join<F>
where
F: Future<Output = T>,
{
type Output = Vec<T>;
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.project().inner.poll(cx)
}
}
pub trait JoinIterExt<T, F>: Iterator
where
F: Future<Output = T>,
{
/// Returns a future that resolves to a vector of the outputs of the futures
/// in the iterator.
fn join(self) -> Join<F>;
}
pin_project! {
/// Future for the [TryJoinIterExt::try_join] method.
#[must_use]
pub struct TryJoin<F>
where
F: Future,
{
#[pin]
inner: JoinAll<F>,
}
}
impl<T, F> Future for TryJoin<F>
where
F: Future<Output = Result<T>>,
{
type Output = Result<Vec<T>>;
fn poll(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
match self.project().inner.poll_unpin(cx) {
std::task::Poll::Ready(res) => {
std::task::Poll::Ready(res.into_iter().collect::<Result<Vec<_>>>())
}
std::task::Poll::Pending => std::task::Poll::Pending,
}
}
}
pub trait TryJoinIterExt<T, F>: Iterator
where
F: Future<Output = Result<T>>,
{
/// 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<F>;
}
impl<T, F, IF, It> JoinIterExt<T, F> for It
where
F: Future<Output = T>,
IF: IntoFuture<Output = T, IntoFuture = F>,
It: Iterator<Item = IF>,
{
fn join(self) -> Join<F> {
Join {
inner: join_all(self.map(|f| f.into_future())),
}
}
}
impl<T, F, IF, It> TryJoinIterExt<T, F> for It
where
F: Future<Output = Result<T>>,
IF: IntoFuture<Output = Result<T>, IntoFuture = F>,
It: Iterator<Item = IF>,
{
fn try_join(self) -> TryJoin<F> {
TryJoin {
inner: join_all(self.map(|f| f.into_future())),
}
}
}
pin_project! {
/// Future for the [TryFlatJoinIterExt::try_flat_join] method.
pub struct TryFlatJoin<F>
where
F: Future,
{
#[pin]
inner: JoinAll<F>,
}
}
impl<F, I, U> Future for TryFlatJoin<F>
where
F: Future<Output = Result<I>>,
I: IntoIterator<IntoIter = U, Item = U::Item>,
U: Iterator,
{
type Output = Result<Vec<U::Item>>;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
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<F, I, U>: Iterator
where
F: Future<Output = Result<I>>,
I: IntoIterator<IntoIter = U, Item = U::Item>,
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<F>;
}
impl<F, IF, It, I, U> TryFlatJoinIterExt<F, I, U> for It
where
F: Future<Output = Result<I>>,
IF: IntoFuture<Output = Result<I>, IntoFuture = F>,
It: Iterator<Item = IF>,
I: IntoIterator<IntoIter = U, Item = U::Item>,
U: Iterator,
{
fn try_flat_join(self) -> TryFlatJoin<F> {
TryFlatJoin {
inner: join_all(self.map(|f| f.into_future())),
}
}
}
|