Function fibers::fiber::yield_poll [] [src]

pub fn yield_poll<T, E>() -> Poll<T, E>

Cooperatively gives up a poll for the current future (fiber).

Examples

use fibers::{fiber, Executor, InPlaceExecutor, Spawn};
use futures::{Future, Async, Poll};

struct HeavyCalculation {
    polled_count: usize,
    loops: usize
}
impl HeavyCalculation {
    fn new(loop_count: usize) -> Self {
        HeavyCalculation { polled_count: 0, loops: loop_count }
    }
}
impl Future for HeavyCalculation {
    type Item = usize;
    type Error = ();
    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.polled_count += 1;

        let mut per_poll_loop_limit = 10;
        while self.loops > 0 {
            self.loops -= 1;
            per_poll_loop_limit -= 1;
            if per_poll_loop_limit == 0 {
                // Suspends calculation and gives execution to other fibers.
                return fiber::yield_poll();
            }
        }
        Ok(Async::Ready(self.polled_count))
    }
}

let mut executor = InPlaceExecutor::new().unwrap();
let monitor = executor.spawn_monitor(HeavyCalculation::new(100));
let result = executor.run_fiber(monitor).unwrap();
assert_eq!(result, Ok(11));