|
| 1 | +use crate::stream::{Fuse, FuturesUnordered, StreamExt}; |
| 2 | +use crate::try_future::{IntoFuture, TryFutureExt}; |
| 3 | +use crate::try_stream::IntoStream; |
| 4 | +use futures_core::future::TryFuture; |
| 5 | +use futures_core::stream::{Stream, TryStream}; |
| 6 | +use futures_core::task::{self, Poll}; |
| 7 | +use std::marker::Unpin; |
| 8 | +use std::mem::PinMut; |
| 9 | + |
| 10 | +/// A stream returned by the |
| 11 | +/// [`try_buffer_unordered`](super::TryStreamExt::try_buffer_unordered) method |
| 12 | +#[derive(Debug)] |
| 13 | +#[must_use = "streams do nothing unless polled"] |
| 14 | +pub struct TryBufferUnordered<St> |
| 15 | + where St: TryStream |
| 16 | +{ |
| 17 | + stream: Fuse<IntoStream<St>>, |
| 18 | + in_progress_queue: FuturesUnordered<IntoFuture<St::Ok>>, |
| 19 | + max: usize, |
| 20 | +} |
| 21 | + |
| 22 | +impl<St> Unpin for TryBufferUnordered<St> |
| 23 | + where St: TryStream + Unpin |
| 24 | +{} |
| 25 | + |
| 26 | +impl<St> TryBufferUnordered<St> |
| 27 | + where St: TryStream, |
| 28 | + St::Ok: TryFuture, |
| 29 | +{ |
| 30 | + unsafe_pinned!(stream: Fuse<IntoStream<St>>); |
| 31 | + unsafe_unpinned!(in_progress_queue: FuturesUnordered<IntoFuture<St::Ok>>); |
| 32 | + |
| 33 | + pub(super) fn new(stream: St, n: usize) -> Self { |
| 34 | + TryBufferUnordered { |
| 35 | + stream: IntoStream::new(stream).fuse(), |
| 36 | + in_progress_queue: FuturesUnordered::new(), |
| 37 | + max: n, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Acquires a reference to the underlying stream that this combinator is |
| 42 | + /// pulling from. |
| 43 | + pub fn get_ref(&self) -> &St { |
| 44 | + self.stream.get_ref().get_ref() |
| 45 | + } |
| 46 | + |
| 47 | + /// Acquires a mutable reference to the underlying stream that this |
| 48 | + /// combinator is pulling from. |
| 49 | + /// |
| 50 | + /// Note that care must be taken to avoid tampering with the state of the |
| 51 | + /// stream which may otherwise confuse this combinator. |
| 52 | + pub fn get_mut(&mut self) -> &mut St { |
| 53 | + self.stream.get_mut().get_mut() |
| 54 | + } |
| 55 | + |
| 56 | + /// Consumes this combinator, returning the underlying stream. |
| 57 | + /// |
| 58 | + /// Note that this may discard intermediate state of this combinator, so |
| 59 | + /// care should be taken to avoid losing resources when this is called. |
| 60 | + pub fn into_inner(self) -> St { |
| 61 | + self.stream.into_inner().into_inner() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<St> Stream for TryBufferUnordered<St> |
| 66 | + where St: TryStream, |
| 67 | + St::Ok: TryFuture<Error = St::Error>, |
| 68 | +{ |
| 69 | + type Item = Result<<St::Ok as TryFuture>::Ok, St::Error>; |
| 70 | + |
| 71 | + fn poll_next( |
| 72 | + mut self: PinMut<Self>, |
| 73 | + cx: &mut task::Context, |
| 74 | + ) -> Poll<Option<Self::Item>> { |
| 75 | + // First up, try to spawn off as many futures as possible by filling up |
| 76 | + // our slab of futures. Propagate errors from the stream immediately. |
| 77 | + while self.in_progress_queue.len() < self.max { |
| 78 | + match self.stream().poll_next(cx) { |
| 79 | + Poll::Ready(Some(Ok(fut))) => self.in_progress_queue().push(fut.into_future()), |
| 80 | + Poll::Ready(Some(Err(e))) => return Poll::Ready(Some(Err(e))), |
| 81 | + Poll::Ready(None) | Poll::Pending => break, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Attempt to pull the next value from the in_progress_queue |
| 86 | + match PinMut::new(self.in_progress_queue()).poll_next(cx) { |
| 87 | + x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x, |
| 88 | + Poll::Ready(None) => {} |
| 89 | + } |
| 90 | + |
| 91 | + // If more values are still coming from the stream, we're not done yet |
| 92 | + if self.stream.is_done() { |
| 93 | + Poll::Ready(None) |
| 94 | + } else { |
| 95 | + Poll::Pending |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments