use futures::{Async, Future};
use std::io;
pub use self::in_place::{InPlaceExecutor, InPlaceExecutorHandle};
pub use self::thread_pool::{ThreadPoolExecutor, ThreadPoolExecutorHandle};
use fiber::Spawn;
use sync::oneshot::{Monitor, MonitorError};
mod in_place;
mod thread_pool;
pub trait Executor: Sized {
type Handle: Spawn + Clone + Send + 'static;
fn handle(&self) -> Self::Handle;
fn run_once(&mut self) -> io::Result<()>;
fn run_fiber<T, E>(
&mut self,
monitor: Monitor<T, E>,
) -> io::Result<Result<T, MonitorError<E>>> {
self.run_future(monitor)
}
fn run_future<F: Future>(&mut self, mut future: F) -> io::Result<Result<F::Item, F::Error>> {
loop {
match future.poll() {
Err(e) => return Ok(Err(e)),
Ok(Async::Ready(v)) => return Ok(Ok(v)),
Ok(Async::NotReady) => {}
}
self.run_once()?;
}
}
fn run(mut self) -> io::Result<()> {
loop {
self.run_once()?
}
}
}