@@ -1,143 +0,0 @@
1−//! An unbounded set of streams
9−use std::task::{Context, Poll};
11−use futures_util::stream::{FusedStream, FuturesUnordered, StreamExt, StreamFuture};
12−use futures_util::{ready, stream::Stream};
14−/// An unbounded set of streams
16−/// This "combinator" provides the ability to maintain a set of streams
17−/// and drive them all to completion.
19−/// Streams are pushed into this set and their realized values are
20−/// yielded as they become ready. Streams will only be polled when they
21−/// generate notifications. This allows to coordinate a large number of streams.
23−/// Note that you can create a ready-made `SelectAll` via the
24−/// `select_all` function in the `stream` module, or you can start with an
25−/// empty set with the `SelectAll::new` constructor.
26−#[must_use = "streams do nothing unless polled"]
27−pub struct SelectAll<St> {
28− inner: FuturesUnordered<StreamFuture<St>>,
31−impl<St: Debug> Debug for SelectAll<St> {
32− fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33− write!(f, "SelectAll {{ ... }}")
37−impl<St: Stream + Unpin> SelectAll<St> {
38− /// Constructs a new, empty `SelectAll`
40− /// The returned `SelectAll` does not contain any streams and, in this
41− /// state, `SelectAll::poll` will return `Poll::Ready(None)`.
42− pub fn new() -> Self {
44− inner: FuturesUnordered::new(),
48− /// Returns the number of streams contained in the set.
50− /// This represents the total number of in-flight streams.
51− pub fn len(&self) -> usize {
55− /// Returns `true` if the set contains no streams
56− pub fn is_empty(&self) -> bool {
57− self.inner.is_empty()
60− /// Push a stream into the set.
62− /// This function submits the given stream to the set for managing. This
63− /// function will not call `poll` on the submitted stream. The caller must
64− /// ensure that `SelectAll::poll` is called in order to receive task
66− pub fn push(&self, stream: St) {
67− self.inner.push(stream.into_future());
71−impl<St: Stream + Unpin> Default for SelectAll<St> {
72− fn default() -> Self {
77−impl<St: Stream + Unpin> Stream for SelectAll<St> {
78− type Item = St::Item;
80− fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
82− match ready!(self.inner.poll_next_unpin(cx)) {
83− Some((Some(item), remaining)) => {
84− self.push(remaining);
85− return Poll::Ready(Some(item));
88− // `FuturesUnordered` thinks it isn't terminated
89− // because it yielded a Some.
90− // We do not return, but poll `FuturesUnordered`
91− // in the next loop iteration.
93− None => return Poll::Ready(None),
99−impl<St: Stream + Unpin> FusedStream for SelectAll<St> {
100− fn is_terminated(&self) -> bool {
101− self.inner.is_terminated()
105−/// Convert a list of streams into a `Stream` of results from the streams.
107−/// This essentially takes a list of streams (e.g. a vector, an iterator, etc.)
108−/// and bundles them together into a single stream.
109−/// The stream will yield items as they become available on the underlying
110−/// streams internally, in the order they become available.
112−/// Note that the returned set can also be used to dynamically push more
113−/// futures into the set as they become available.
115−/// This function is only available when the `std` or `alloc` feature of this
116−/// library is activated, and it is activated by default.
117−pub fn select_all<I>(streams: I) -> SelectAll<I::Item>
120− I::Item: Stream + Unpin,
122− let set = SelectAll::new();
124− for stream in streams {
131−impl<St: Stream + Unpin> FromIterator<St> for SelectAll<St> {
132− fn from_iter<T: IntoIterator<Item = St>>(iter: T) -> Self {
137−impl<St: Stream + Unpin> Extend<St> for SelectAll<St> {
138− fn extend<T: IntoIterator<Item = St>>(&mut self, iter: T) {