/// An unbounded set of streams /// /// This "combinator" provides the ability to maintain a set of streams /// and drive them all to completion. /// /// Streams are pushed into this set and their realized values are /// yielded as they become ready. Streams will only be polled when they /// generate notifications. This allows to coordinate a large number of streams. /// /// Note that you can create a ready-made `SelectAll` via the /// `select_all` function in the `stream` module, or you can start with an /// empty set with the `SelectAll::new` constructor. #[must_use = "streams do nothing unless polled"] pubstruct SelectAll<St> {
inner: FuturesUnordered<StreamFuture<St>>,
}
impl<St: Stream + Unpin> SelectAll<St> { /// Constructs a new, empty `SelectAll` /// /// The returned `SelectAll` does not contain any streams and, in this /// state, `SelectAll::poll` will return `Poll::Ready(None)`. pubfn new() -> Self { Self { inner: FuturesUnordered::new() }
}
/// Returns the number of streams contained in the set. /// /// This represents the total number of in-flight streams. pubfn len(&self) -> usize { self.inner.len()
}
/// Returns `true` if the set contains no streams pubfn is_empty(&self) -> bool { self.inner.is_empty()
}
/// Push a stream into the set. /// /// This function submits the given stream to the set for managing. This /// function will not call `poll` on the submitted stream. The caller must /// ensure that `SelectAll::poll` is called in order to receive task /// notifications. pubfn push(&mutself, stream: St) { self.inner.push(stream.into_future());
}
/// Returns an iterator that allows inspecting each stream in the set. pubfn iter(&self) -> Iter<'_, St> {
Iter(self.inner.iter())
}
/// Returns an iterator that allows modifying each stream in the set. pubfn iter_mut(&mutself) -> IterMut<'_, St> {
IterMut(self.inner.iter_mut())
}
/// Clears the set, removing all streams. pubfn clear(&mutself) { self.inner.clear()
}
}
/// Convert a list of streams into a `Stream` of results from the streams. /// /// This essentially takes a list of streams (e.g. a vector, an iterator, etc.) /// and bundles them together into a single stream. /// The stream will yield items as they become available on the underlying /// streams internally, in the order they become available. /// /// Note that the returned set can also be used to dynamically push more /// streams into the set as they become available. /// /// This function is only available when the `std` or `alloc` feature of this /// library is activated, and it is activated by default. pubfn select_all<I>(streams: I) -> SelectAll<I::Item> where
I: IntoIterator,
I::Item: Stream + Unpin,
{ letmut set = SelectAll::new();
for stream in streams {
set.push(stream);
}
assert_stream::<<I::Item as Stream>::Item, _>(set)
}
impl<St: Stream + Unpin> Extend<St> for SelectAll<St> { fn extend<T: IntoIterator<Item = St>>(&mutself, iter: T) { for st in iter { self.push(st)
}
}
}
impl<St: Stream + Unpin> IntoIterator for SelectAll<St> { type Item = St; type IntoIter = IntoIter<St>;
/// Immutable iterator over all streams in the unordered set. #[derive(Debug)] pubstruct Iter<'a, St: Unpin>(futures_unordered::Iter<'a, StreamFuture<St>>);
/// Mutable iterator over all streams in the unordered set. #[derive(Debug)] pubstruct IterMut<'a, St: Unpin>(futures_unordered::IterMut<'a, StreamFuture<St>>);
/// Owned iterator over all streams in the unordered set. #[derive(Debug)] pubstruct IntoIter<St: Unpin>(futures_unordered::IntoIter<StreamFuture<St>>);
impl<'a, St: Stream + Unpin> Iterator for Iter<'a, St> { type Item = &'a St;
fn next(&mutself) -> Option<Self::Item> { let st = self.0.next()?; let next = st.get_ref(); // This should always be true because FuturesUnordered removes completed futures.
debug_assert!(next.is_some());
next
}
impl<St: Stream + Unpin> ExactSizeIterator for Iter<'_, St> {}
impl<'a, St: Stream + Unpin> Iterator for IterMut<'a, St> { type Item = &'a mut St;
fn next(&mutself) -> Option<Self::Item> { let st = self.0.next()?; let next = st.get_mut(); // This should always be true because FuturesUnordered removes completed futures.
debug_assert!(next.is_some());
next
}
impl<St: Stream + Unpin> ExactSizeIterator for IterMut<'_, St> {}
impl<St: Stream + Unpin> Iterator for IntoIter<St> { type Item = St;
fn next(&mutself) -> Option<Self::Item> { let st = self.0.next()?; let next = st.into_inner(); // This should always be true because FuturesUnordered removes completed futures.
debug_assert!(next.is_some());
next
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.