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
use core::{fmt, num::NonZeroUsize, pin::Pin};
use futures_core::{
    future::{FusedFuture, Future},
    stream::TryStream,
    task::{Context, Poll},
};
use futures_util::stream::{FuturesUnordered, StreamExt};
use pin_project_lite::pin_project;

pin_project! {
    /// Future for the
    /// [`for_each_concurrent_then_try`](super::TryStreamExt::for_each_concurrent_then_try)
    /// method.
    #[must_use = "futures do nothing unless you `.await` or poll them"]
    pub struct ForEachConcurrentThenTry<St: TryStream, Fut, F> {
        #[pin]
        stream: Option<St>,
        f: F,
        futures: FuturesUnordered<Fut>,
        limit: Option<NonZeroUsize>,
        first_error: Option<St::Error>,
    }
}

impl<St, Fut, F> fmt::Debug for ForEachConcurrentThenTry<St, Fut, F>
where
    St: TryStream + fmt::Debug,
    Fut: fmt::Debug,
    St::Error: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ForEachConcurrentThenTry")
            .field("stream", &self.stream)
            .field("futures", &self.futures)
            .field("limit", &self.limit)
            .field("first_error", &self.first_error)
            .finish()
    }
}

impl<St, Fut, F> FusedFuture for ForEachConcurrentThenTry<St, Fut, F>
where
    St: TryStream,
    F: FnMut(St::Ok) -> Fut,
    Fut: Future<Output = Result<(), St::Error>>,
{
    fn is_terminated(&self) -> bool {
        self.stream.is_none() && self.futures.is_empty()
    }
}

impl<St, Fut, F> ForEachConcurrentThenTry<St, Fut, F>
where
    St: TryStream,
    F: FnMut(St::Ok) -> Fut,
    Fut: Future<Output = Result<(), St::Error>>,
{
    pub(super) fn new(stream: St, limit: Option<usize>, f: F) -> Self {
        Self {
            stream: Some(stream),
            // Note: `limit` = 0 gets ignored.
            limit: limit.and_then(NonZeroUsize::new),
            f,
            futures: FuturesUnordered::new(),
            first_error: None,
        }
    }
}

impl<St, Fut, F> Future for ForEachConcurrentThenTry<St, Fut, F>
where
    St: TryStream,
    F: FnMut(St::Ok) -> Fut,
    Fut: Future<Output = Result<(), St::Error>>,
{
    type Output = Result<(), St::Error>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();
        loop {
            let mut made_progress_this_iter = false;

            // Check if we've already created a number of futures greater than `limit`
            if this
                .limit
                .map(|limit| limit.get() > this.futures.len())
                .unwrap_or(true)
            {
                let mut stream_completed = false;
                let elem = if let Some(stream) = this.stream.as_mut().as_pin_mut() {
                    match stream.try_poll_next(cx) {
                        Poll::Ready(Some(Ok(elem))) => {
                            made_progress_this_iter = true;
                            Some(elem)
                        }
                        Poll::Ready(Some(Err(error))) => {
                            if this.first_error.is_none() {
                                *this.first_error = Some(error);
                            }
                            None
                        }
                        Poll::Ready(None) => {
                            stream_completed = true;
                            None
                        }
                        Poll::Pending => None,
                    }
                } else {
                    None
                };
                if stream_completed {
                    this.stream.set(None);
                }
                if let Some(elem) = elem {
                    this.futures.push((this.f)(elem));
                }
            }

            match this.futures.poll_next_unpin(cx) {
                Poll::Ready(Some(item)) => {
                    made_progress_this_iter = true;
                    if let Err(error) = item {
                        if this.first_error.is_none() {
                            *this.first_error = Some(error);
                        }
                    }
                }
                Poll::Ready(None) => {
                    if this.stream.is_none() {
                        return Poll::Ready(this.first_error.take().map_or(Ok(()), Err));
                    }
                }
                Poll::Pending => {}
            }

            if !made_progress_this_iter {
                return Poll::Pending;
            }
        }
    }
}