1use futures::{Async, Future};
6use std::io;
7
8pub use self::in_place::{InPlaceExecutor, InPlaceExecutorHandle};
9pub use self::thread_pool::{ThreadPoolExecutor, ThreadPoolExecutorHandle};
10
11use fiber::Spawn;
12use sync::oneshot::{Monitor, MonitorError};
13
14mod in_place;
15mod thread_pool;
16
17pub trait Executor: Sized {
19 type Handle: Spawn + Clone + Send + 'static;
21
22 fn handle(&self) -> Self::Handle;
24
25 fn run_once(&mut self) -> io::Result<()>;
27
28 fn run_fiber<T, E>(
30 &mut self,
31 monitor: Monitor<T, E>,
32 ) -> io::Result<Result<T, MonitorError<E>>> {
33 self.run_future(monitor)
34 }
35
36 fn run_future<F: Future>(&mut self, mut future: F) -> io::Result<Result<F::Item, F::Error>> {
38 loop {
39 match future.poll() {
40 Err(e) => return Ok(Err(e)),
41 Ok(Async::Ready(v)) => return Ok(Ok(v)),
42 Ok(Async::NotReady) => {}
43 }
44 self.run_once()?;
45 }
46 }
47
48 fn run(mut self) -> io::Result<()> {
50 loop {
51 self.run_once()?
52 }
53 }
54}