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

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

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

Examples

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

let mut executor = InPlaceExecutor::new().unwrap();
let (tx0, rx0) = oneshot::channel();
let (tx1, rx1) = oneshot::channel();

// Spawns receiver
let mut monitor = executor.spawn_monitor(rx0.and_then(move |v| {
    assert_eq!(v, "first value");
    rx1
})
.and_then(|v| {
    assert_eq!(v, "second value");
    Ok(())
}));

// Spawns sender for `tx1`
executor.spawn_fn(move || {
    tx1.send("second value").unwrap();
    Ok(())
});

// It is allowed to send messages from the outside of a fiber.
// (The same is true of receiving)
tx0.send("first value").unwrap();

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