|
| 1 | +use crate::stream::{Fuse, FuturesUnordered, StreamExt}; |
| 2 | +use futures_core::future::Future; |
| 3 | +use futures_core::stream::{Stream, FusedStream}; |
| 4 | +use futures_core::task::{Context, Poll}; |
| 5 | +#[cfg(feature = "sink")] |
| 6 | +use futures_sink::Sink; |
| 7 | +use pin_utils::{unsafe_pinned, unsafe_unpinned}; |
| 8 | +use core::fmt; |
| 9 | +use core::pin::Pin; |
| 10 | +use core::sync::atomic::{AtomicUsize, Ordering}; |
| 11 | +use alloc::sync::Arc; |
| 12 | + |
| 13 | +/// Stream for the [`buffer_unordered_adaptable`](super::StreamExt::buffer_unordered_adaptable) |
| 14 | +/// method. |
| 15 | +#[must_use = "streams do nothing unless polled"] |
| 16 | +pub struct BufferUnorderedAdaptable<St> |
| 17 | +where |
| 18 | + St: Stream, |
| 19 | +{ |
| 20 | + stream: Fuse<St>, |
| 21 | + in_progress_queue: FuturesUnordered<St::Item>, |
| 22 | + max: Arc<AtomicUsize>, |
| 23 | +} |
| 24 | + |
| 25 | +impl<St> Unpin for BufferUnorderedAdaptable<St> |
| 26 | +where |
| 27 | + St: Stream + Unpin, |
| 28 | +{} |
| 29 | + |
| 30 | +impl<St> fmt::Debug for BufferUnorderedAdaptable<St> |
| 31 | +where |
| 32 | + St: Stream + fmt::Debug, |
| 33 | +{ |
| 34 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 35 | + f.debug_struct("BufferUnorderedAdaptable") |
| 36 | + .field("stream", &self.stream) |
| 37 | + .field("in_progress_queue", &self.in_progress_queue) |
| 38 | + .field("max", &self.max.load(Ordering::Relaxed)) |
| 39 | + .finish() |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<St> BufferUnorderedAdaptable<St> |
| 44 | +where |
| 45 | + St: Stream, |
| 46 | + St::Item: Future, |
| 47 | +{ |
| 48 | + unsafe_pinned!(stream: Fuse<St>); |
| 49 | + unsafe_unpinned!(in_progress_queue: FuturesUnordered<St::Item>); |
| 50 | + |
| 51 | + pub(super) fn new(stream: St, n: Arc<AtomicUsize>) -> BufferUnorderedAdaptable<St> |
| 52 | + where |
| 53 | + St: Stream, |
| 54 | + St::Item: Future, |
| 55 | + { |
| 56 | + BufferUnorderedAdaptable { |
| 57 | + stream: super::Fuse::new(stream), |
| 58 | + in_progress_queue: FuturesUnordered::new(), |
| 59 | + max: n, |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + /// Acquires a reference to the underlying stream that this combinator is |
| 64 | + /// pulling from. |
| 65 | + pub fn get_ref(&self) -> &St { |
| 66 | + self.stream.get_ref() |
| 67 | + } |
| 68 | + |
| 69 | + /// Acquires a mutable reference to the underlying stream that this |
| 70 | + /// combinator is pulling from. |
| 71 | + /// |
| 72 | + /// Note that care must be taken to avoid tampering with the state of the |
| 73 | + /// stream which may otherwise confuse this combinator. |
| 74 | + pub fn get_mut(&mut self) -> &mut St { |
| 75 | + self.stream.get_mut() |
| 76 | + } |
| 77 | + |
| 78 | + /// Acquires a pinned mutable reference to the underlying stream that this |
| 79 | + /// combinator is pulling from. |
| 80 | + /// |
| 81 | + /// Note that care must be taken to avoid tampering with the state of the |
| 82 | + /// stream which may otherwise confuse this combinator. |
| 83 | + pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut St> { |
| 84 | + self.stream().get_pin_mut() |
| 85 | + } |
| 86 | + |
| 87 | + /// Consumes this combinator, returning the underlying stream. |
| 88 | + /// |
| 89 | + /// Note that this may discard intermediate state of this combinator, so |
| 90 | + /// care should be taken to avoid losing resources when this is called. |
| 91 | + pub fn into_inner(self) -> St { |
| 92 | + self.stream.into_inner() |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<St> Stream for BufferUnorderedAdaptable<St> |
| 97 | +where |
| 98 | + St: Stream, |
| 99 | + St::Item: Future, |
| 100 | +{ |
| 101 | + type Item = <St::Item as Future>::Output; |
| 102 | + |
| 103 | + fn poll_next( |
| 104 | + mut self: Pin<&mut Self>, |
| 105 | + cx: &mut Context<'_>, |
| 106 | + ) -> Poll<Option<Self::Item>> { |
| 107 | + // First up, try to spawn off as many futures as possible by filling up |
| 108 | + // our queue of futures. |
| 109 | + while self.in_progress_queue.len() < self.max.load(Ordering::Relaxed) { |
| 110 | + match self.as_mut().stream().poll_next(cx) { |
| 111 | + Poll::Ready(Some(fut)) => self.as_mut().in_progress_queue().push(fut), |
| 112 | + Poll::Ready(None) | Poll::Pending => break, |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Attempt to pull the next value from the in_progress_queue |
| 117 | + match self.as_mut().in_progress_queue().poll_next_unpin(cx) { |
| 118 | + x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x, |
| 119 | + Poll::Ready(None) => {} |
| 120 | + } |
| 121 | + |
| 122 | + // If more values are still coming from the stream, we're not done yet |
| 123 | + if self.stream.is_done() { |
| 124 | + Poll::Ready(None) |
| 125 | + } else { |
| 126 | + Poll::Pending |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 131 | + let queue_len = self.in_progress_queue.len(); |
| 132 | + let (lower, upper) = self.stream.size_hint(); |
| 133 | + let lower = lower.saturating_add(queue_len); |
| 134 | + let upper = match upper { |
| 135 | + Some(x) => x.checked_add(queue_len), |
| 136 | + None => None, |
| 137 | + }; |
| 138 | + (lower, upper) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +impl<St> FusedStream for BufferUnorderedAdaptable<St> |
| 143 | +where |
| 144 | + St: Stream, |
| 145 | + St::Item: Future, |
| 146 | +{ |
| 147 | + fn is_terminated(&self) -> bool { |
| 148 | + self.in_progress_queue.is_terminated() && self.stream.is_terminated() |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +// Forwarding impl of Sink from the underlying stream |
| 153 | +#[cfg(feature = "sink")] |
| 154 | +impl<S, Item> Sink<Item> for BufferUnorderedAdaptable<S> |
| 155 | +where |
| 156 | + S: Stream + Sink<Item>, |
| 157 | + S::Item: Future, |
| 158 | +{ |
| 159 | + type Error = S::Error; |
| 160 | + |
| 161 | + delegate_sink!(stream, Item); |
| 162 | +} |
0 commit comments