1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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()?
}
}
}