|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::VecDeque; |
| 19 | +use std::fmt; |
| 20 | +use std::pin::Pin; |
| 21 | +use std::task::Context; |
| 22 | +use std::task::Poll; |
| 23 | + |
| 24 | +use futures::future::Future; |
| 25 | +use futures::stream::Fuse; |
| 26 | +use futures::stream::FuturesOrdered; |
| 27 | +use futures::stream::Stream; |
| 28 | +use futures::stream::StreamExt; |
| 29 | + |
| 30 | +use pin_project_lite::pin_project; |
| 31 | + |
| 32 | +pin_project! { |
| 33 | + /// Stream for the [`buffer_by_ordered`] method. |
| 34 | + /// |
| 35 | + /// [`buffer_by_ordered`]: crate::StreamExt::buffer_by_ordered |
| 36 | + #[must_use = "streams do nothing unless polled"] |
| 37 | + pub struct BufferByOrdered<St, F> |
| 38 | + where |
| 39 | + St: Stream<Item = (F, usize)>, |
| 40 | + F: Future, |
| 41 | + { |
| 42 | + #[pin] |
| 43 | + stream: Fuse<St>, |
| 44 | + in_progress_queue: FuturesOrdered<SizedFuture<F>>, |
| 45 | + ready_queue: VecDeque<(F::Output, usize)>, |
| 46 | + max_size: usize, |
| 47 | + current_size: usize, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<St, F> fmt::Debug for BufferByOrdered<St, F> |
| 52 | +where |
| 53 | + St: Stream<Item = (F, usize)> + fmt::Debug, |
| 54 | + F: Future, |
| 55 | +{ |
| 56 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 57 | + f.debug_struct("BufferByOrdered") |
| 58 | + .field("stream", &self.stream) |
| 59 | + .field("in_progress_queue", &self.in_progress_queue) |
| 60 | + .field("max_size", &self.max_size) |
| 61 | + .field("current_size", &self.current_size) |
| 62 | + .finish() |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl<St, F> BufferByOrdered<St, F> |
| 67 | +where |
| 68 | + St: Stream<Item = (F, usize)>, |
| 69 | + F: Future, |
| 70 | +{ |
| 71 | + pub(crate) fn new(stream: St, max_size: usize) -> Self { |
| 72 | + Self { |
| 73 | + stream: stream.fuse(), |
| 74 | + in_progress_queue: FuturesOrdered::new(), |
| 75 | + ready_queue: VecDeque::new(), |
| 76 | + max_size, |
| 77 | + current_size: 0, |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +impl<St, F> Stream for BufferByOrdered<St, F> |
| 83 | +where |
| 84 | + St: Stream<Item = (F, usize)>, |
| 85 | + F: Future, |
| 86 | +{ |
| 87 | + type Item = F::Output; |
| 88 | + |
| 89 | + fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { |
| 90 | + let mut this = self.project(); |
| 91 | + |
| 92 | + // First up, try to spawn off as many futures as possible by filling up |
| 93 | + // our queue of futures. |
| 94 | + while this.current_size < this.max_size { |
| 95 | + match this.stream.as_mut().poll_next(cx) { |
| 96 | + Poll::Ready(Some((future, size))) => { |
| 97 | + *this.current_size += size; |
| 98 | + this.in_progress_queue |
| 99 | + .push_back(SizedFuture { future, size }); |
| 100 | + } |
| 101 | + Poll::Ready(None) => break, |
| 102 | + Poll::Pending => break, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Try to poll all ready futures in the in_progress_queue. |
| 107 | + loop { |
| 108 | + match this.in_progress_queue.poll_next_unpin(cx) { |
| 109 | + Poll::Ready(Some(output)) => { |
| 110 | + this.ready_queue.push_back(output); |
| 111 | + } |
| 112 | + Poll::Ready(None) => break, |
| 113 | + Poll::Pending => break, |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if let Some((output, size)) = this.ready_queue.pop_front() { |
| 118 | + // If we have any ready outputs, return the first one. |
| 119 | + *this.current_size -= size; |
| 120 | + Poll::Ready(Some(output)) |
| 121 | + } else if this.stream.is_done() && this.in_progress_queue.is_empty() { |
| 122 | + Poll::Ready(None) |
| 123 | + } else { |
| 124 | + Poll::Pending |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 129 | + let queue_len = self.in_progress_queue.len() + self.ready_queue.len(); |
| 130 | + let (lower, upper) = self.stream.size_hint(); |
| 131 | + let lower = lower.saturating_add(queue_len); |
| 132 | + let upper = match upper { |
| 133 | + Some(x) => x.checked_add(queue_len), |
| 134 | + None => None, |
| 135 | + }; |
| 136 | + (lower, upper) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +pin_project! { |
| 141 | + struct SizedFuture<F> { |
| 142 | + #[pin] |
| 143 | + future: F, |
| 144 | + size: usize, |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +impl<F: Future> Future for SizedFuture<F> { |
| 149 | + type Output = (F::Output, usize); |
| 150 | + |
| 151 | + fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { |
| 152 | + let this = self.project(); |
| 153 | + match this.future.poll(cx) { |
| 154 | + Poll::Ready(output) => Poll::Ready((output, *this.size)), |
| 155 | + Poll::Pending => Poll::Pending, |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments