[][src]Type Definition unicycle::IndexedStreamsUnordered

type IndexedStreamsUnordered<T> = Unordered<T, IndexedStreams>;

A container for an unordered collection of Streams, which also yields the index that produced the next item.

Examples

use tokio::{net::TcpListener, time};
use tokio_util::codec::{Framed, LengthDelimitedCodec};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let mut listener = TcpListener::bind("127.0.0.1:8080").await?;
    let mut clients = unicycle::IndexedStreamsUnordered::new();

    loop {
        tokio::select! {
            result = listener.accept() => {
                let (stream, _) = result?;
                clients.push(Framed::new(stream, LengthDelimitedCodec::new()));
            },
            Some((index, frame)) = clients.next() => {
                match frame {
                    Some(frame) => println!("{}: received frame: {:?}", index, frame),
                    None => println!("{}: client disconnected", index),
                }
            }
        }
    }
}

Implementations

impl<F> IndexedStreamsUnordered<F>[src]

pub fn new() -> Self[src]

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 unicycle::IndexedStreamsUnordered;
use tokio::stream::iter;

#[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

impl<T> PollNext for IndexedStreamsUnordered<T> where
    T: Stream
[src]

type Item = (usize, Option<T::Item>)

The output of the poll.