pub struct Unordered<T, S>where
S: Sentinel,{ /* private fields */ }
Expand description
A container for an unordered collection of Futures or Streams.
You should use one of the following type aliases to construct it:
§Examples
use std::time::Duration;
use tokio::time;
let mut futures = unicycle::FuturesUnordered::new();
futures.push(time::sleep(Duration::from_secs(2)));
futures.push(time::sleep(Duration::from_secs(3)));
futures.push(time::sleep(Duration::from_secs(1)));
while let Some(_) = futures.next().await {
println!("tick");
}
println!("done!");
}
Implementations§
Source§impl<T, S> Unordered<T, S>
impl<T, S> Unordered<T, S>
Sourcepub fn next(&mut self) -> Next<'_, Self> ⓘ
pub fn next(&mut self) -> Next<'_, Self> ⓘ
Creates a future that resolves to the next item in the unordered set.
Functions like StreamExt::next
would from the futures crate, but
doesn’t depend on the Stream trait.
§Examples
use std::time::Duration;
use tokio::time;
use unicycle::FuturesUnordered;
let mut futures = FuturesUnordered::new();
futures.push(time::sleep(Duration::from_secs(2)));
futures.push(time::sleep(Duration::from_secs(3)));
futures.push(time::sleep(Duration::from_secs(1)));
while let Some(_) = futures.next().await {
println!("tick");
}
println!("done!");
Source§impl<T> Unordered<T, Futures>
impl<T> Unordered<T, Futures>
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct a new, empty FuturesUnordered.
§Examples
use unicycle::FuturesUnordered;
let mut futures = FuturesUnordered::new();
assert!(futures.is_empty());
futures.push(async { 42 });
Source§impl<T, S> Unordered<T, S>where
S: Sentinel,
impl<T, S> Unordered<T, S>where
S: Sentinel,
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of elements in the collection of futures.
§Examples
use std::future::Ready;
use unicycle::FuturesUnordered;
let mut futures = FuturesUnordered::<Ready<()>>::new();
assert_eq!(futures.len(), 0);
assert!(futures.is_empty());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Test if the collection of futures is empty.
§Examples
use std::future::Ready;
use unicycle::FuturesUnordered;
let mut futures = FuturesUnordered::<Ready<()>>::new();
assert!(futures.is_empty());
Sourcepub fn push(&mut self, future: T) -> usize
pub fn push(&mut self, future: T) -> usize
Push the given future or stream to Unordered and return its task index.
Newly added futures are guaranteed to be polled, but there is no guarantee in which order this will happen.
Pushed tasks are pinned by the Unordered collection automatically.
§Examples
use unicycle::FuturesUnordered;
let mut futures = FuturesUnordered::new();
assert!(futures.is_empty());
futures.push(async { 42 });
assert!(!futures.is_empty());
Sourcepub fn get_pin_mut(&mut self, index: usize) -> Option<Pin<&mut T>>
pub fn get_pin_mut(&mut self, index: usize) -> Option<Pin<&mut T>>
Get a pinned mutable reference to the stream or future at the given index.
§Examples
use std::future::Future;
use unicycle::FuturesUnordered;
use futures::future::poll_fn;
let mut futures = FuturesUnordered::new();
let index = futures.push(async { 42 });
let result = poll_fn(|cx| {
futures.get_pin_mut(index).expect("expected future").poll(cx)
}).await;
assert_eq!(result, 42);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>where
T: Unpin,
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>where
T: Unpin,
Get a mutable reference to the stream or future at the given index. Requires that the stores stream or future is Unpin.
§Examples
use std::pin::Pin;
use std::future::Future;
use unicycle::FuturesUnordered;
use futures::future::{ready, poll_fn};
let mut futures = FuturesUnordered::new();
let index = futures.push(ready(42));
let result = poll_fn(|cx| {
Pin::new(futures.get_mut(index).expect("expected future")).poll(cx)
}).await;
assert_eq!(result, 42);
Source§impl<T> Unordered<T, Streams>
impl<T> Unordered<T, Streams>
Sourcepub fn new() -> Self
Available on crate feature futures-rs
only.
pub fn new() -> Self
futures-rs
only.Construct a new, empty StreamsUnordered.
§Examples
use tokio_stream::iter;
use unicycle::StreamsUnordered;
let mut streams = StreamsUnordered::new();
assert!(streams.is_empty());
streams.push(iter(vec![1, 2, 3, 4]));
streams.push(iter(vec![5, 6, 7, 8]));
let mut received = Vec::new();
while let Some(value) = streams.next().await {
received.push(value);
}
assert_eq!(vec![5, 1, 6, 2, 7, 3, 8, 4], received);
Source§impl<T> Unordered<T, IndexedStreams>
impl<T> Unordered<T, IndexedStreams>
Sourcepub fn new() -> Self
Available on crate feature futures-rs
only.
pub fn new() -> Self
futures-rs
only.Construct a new, empty IndexedStreamsUnordered.
This is the same as StreamsUnordered, except that it yields the index of the stream who’se value was just yielded, alongside the yielded value.
§Examples
use tokio_stream::iter;
use unicycle::IndexedStreamsUnordered;
let mut streams = IndexedStreamsUnordered::new();
assert!(streams.is_empty());
streams.push(iter(vec![1, 2]));
streams.push(iter(vec![5, 6]));
let mut received = Vec::new();
while let Some(value) = streams.next().await {
received.push(value);
}
assert_eq!(
vec![
(1, Some(5)),
(0, Some(1)),
(1, Some(6)),
(0, Some(2)),
(1, None),
(0, None)
],
received
);
Trait Implementations§
Source§impl<T, S> Extend<T> for Unordered<T, S>where
S: Sentinel,
impl<T, S> Extend<T> for Unordered<T, S>where
S: Sentinel,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, S> FusedStream for Unordered<T, S>
Available on crate feature futures-rs
only.
impl<T, S> FusedStream for Unordered<T, S>
futures-rs
only.Source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.Source§impl<T, S> Stream for Unordered<T, S>
Available on crate feature futures-rs
only.Provide Stream
implementation through PollNext
.
impl<T, S> Stream for Unordered<T, S>
futures-rs
only.Provide Stream
implementation through PollNext
.