Struct unicycle::Unordered

source ·
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 tokio::time;
use std::time::Duration;

#[tokio::main]
async fn main() {
    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>
where S: Sentinel, Self: PollNext,

source

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 tokio::time;
use std::time::Duration;
use unicycle::FuturesUnordered;

#[tokio::main]
async fn main() {
    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>

source

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,

source

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());
source

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());
source

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());
source

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 unicycle::FuturesUnordered;
use futures::future::poll_fn;
use std::future::Future as _;

#[tokio::main]
async fn main() {
    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);
}
source

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 unicycle::FuturesUnordered;
use futures::future::{ready, poll_fn};
use std::{pin::Pin, future::Future as _};

#[tokio::main]
async fn main() {
    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>

source

pub fn new() -> Self

Available on crate feature futures-rs only.

Construct a new, empty StreamsUnordered.

§Examples
use tokio_stream::iter;
use unicycle::StreamsUnordered;

#[tokio::main]
async fn main() {
    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>

source

pub fn new() -> Self

Available on crate feature 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;

#[tokio::main]
async fn main() {
    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> Default for Unordered<T, Futures>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T, S> Drop for Unordered<T, S>
where S: Sentinel,

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

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>,

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<T, S> FusedStream for Unordered<T, S>
where S: Sentinel, Self: PollNext,

Available on crate feature futures-rs only.
source§

fn is_terminated(&self) -> bool

Returns true if the stream should no longer be polled.
source§

impl<T, S> Stream for Unordered<T, S>
where S: Sentinel, Self: PollNext,

Available on crate feature futures-rs only.

Provide Stream implementation through PollNext.

§

type Item = <Unordered<T, S> as PollNext>::Item

Values yielded by the stream.
source§

fn poll_next( self: Pin<&mut Self>, cx: &mut Context<'_> ) -> Poll<Option<Self::Item>>

Attempt to pull out the next value of this stream, registering the current task for wakeup if the value is not yet available, and returning None if the stream is exhausted. Read more
source§

fn size_hint(&self) -> (usize, Option<usize>)

Returns the bounds on the remaining length of the stream. Read more
source§

impl<T, S> Send for Unordered<T, S>
where T: Send, S: Sentinel,

source§

impl<T, S> Sync for Unordered<T, S>
where T: Sync, S: Sentinel,

source§

impl<T, S> Unpin for Unordered<T, S>
where S: Sentinel,

Auto Trait Implementations§

§

impl<T, S> !RefUnwindSafe for Unordered<T, S>

§

impl<T, S> !UnwindSafe for Unordered<T, S>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<S, T, E> TryStream for S
where S: Stream<Item = Result<T, E>> + ?Sized,

§

type Ok = T

The type of successful values yielded by this future
§

type Error = E

The type of failures yielded by this future
source§

fn try_poll_next( self: Pin<&mut S>, cx: &mut Context<'_> ) -> Poll<Option<Result<<S as TryStream>::Ok, <S as TryStream>::Error>>>

Poll this TryStream as if it were a Stream. Read more