[][src]Function fibers::sync::mpsc::channel

pub fn channel<T>() -> (Sender<T>, Receiver<T>)

Creates a new asynchronous channel, returning the sender/receiver halves.

All data sent on the sender will become available on the receiver, and no send will block the calling thread (this channel has an "infinite buffer").

Examples

use fibers::{Executor, InPlaceExecutor, Spawn};
use fibers::sync::mpsc;
use futures::{Future, Stream};

let mut executor = InPlaceExecutor::new().unwrap();
let (tx0, rx) = mpsc::channel();

// Spawns receiver
let mut monitor = executor.spawn_monitor(rx.for_each(|m| {
    println!("# Recv: {:?}", m);
    Ok(())
}));

// Spawns sender
let tx1 = tx0.clone();
executor.spawn_fn(move || {
    tx1.send(1).unwrap();
    tx1.send(2).unwrap();
    Ok(())
});

// It is allowed to send messages from the outside of a fiber.
// (The same is true of receiving)
tx0.send(0).unwrap();
std::mem::drop(tx0);

// Runs `executor` until the receiver exits (i.e., channel is disconnected)
while monitor.poll().unwrap().is_not_ready() {
    executor.run_once().unwrap();
}