• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::stream::{Fuse, FuturesOrdered, StreamExt, IntoStream};
2 use crate::future::{IntoFuture, TryFutureExt};
3 use futures_core::future::TryFuture;
4 use futures_core::stream::{Stream, TryStream};
5 use futures_core::task::{Context, Poll};
6 #[cfg(feature = "sink")]
7 use futures_sink::Sink;
8 use pin_project_lite::pin_project;
9 use core::pin::Pin;
10 
11 pin_project! {
12     /// Stream for the [`try_buffered`](super::TryStreamExt::try_buffered) method.
13     #[derive(Debug)]
14     #[must_use = "streams do nothing unless polled"]
15     pub struct TryBuffered<St>
16     where
17         St: TryStream,
18         St::Ok: TryFuture,
19     {
20         #[pin]
21         stream: Fuse<IntoStream<St>>,
22         in_progress_queue: FuturesOrdered<IntoFuture<St::Ok>>,
23         max: usize,
24     }
25 }
26 
27 impl<St> TryBuffered<St>
28 where
29     St: TryStream,
30     St::Ok: TryFuture,
31 {
new(stream: St, n: usize) -> Self32     pub(super) fn new(stream: St, n: usize) -> Self {
33         Self {
34             stream: IntoStream::new(stream).fuse(),
35             in_progress_queue: FuturesOrdered::new(),
36             max: n,
37         }
38     }
39 
40     delegate_access_inner!(stream, St, (. .));
41 }
42 
43 impl<St> Stream for TryBuffered<St>
44 where
45     St: TryStream,
46     St::Ok: TryFuture<Error = St::Error>,
47 {
48     type Item = Result<<St::Ok as TryFuture>::Ok, St::Error>;
49 
poll_next( self: Pin<&mut Self>, cx: &mut Context<'_>, ) -> Poll<Option<Self::Item>>50     fn poll_next(
51         self: Pin<&mut Self>,
52         cx: &mut Context<'_>,
53     ) -> Poll<Option<Self::Item>> {
54         let mut this = self.project();
55 
56         // First up, try to spawn off as many futures as possible by filling up
57         // our queue of futures. Propagate errors from the stream immediately.
58         while this.in_progress_queue.len() < *this.max {
59             match this.stream.as_mut().poll_next(cx)? {
60                 Poll::Ready(Some(fut)) => this.in_progress_queue.push(fut.into_future()),
61                 Poll::Ready(None) | Poll::Pending => break,
62             }
63         }
64 
65         // Attempt to pull the next value from the in_progress_queue
66         match this.in_progress_queue.poll_next_unpin(cx) {
67             x @ Poll::Pending | x @ Poll::Ready(Some(_)) => return x,
68             Poll::Ready(None) => {}
69         }
70 
71         // If more values are still coming from the stream, we're not done yet
72         if this.stream.is_done() {
73             Poll::Ready(None)
74         } else {
75             Poll::Pending
76         }
77     }
78 }
79 
80 // Forwarding impl of Sink from the underlying stream
81 #[cfg(feature = "sink")]
82 impl<S, Item, E> Sink<Item> for TryBuffered<S>
83 where
84     S: TryStream + Sink<Item, Error = E>,
85     S::Ok: TryFuture<Error = E>,
86 {
87     type Error = E;
88 
89     delegate_sink!(stream, Item);
90 }
91