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
// Copyright (c) 2016 DWANGO Co., Ltd. All Rights Reserved.
// See the LICENSE file at the top-level directory of this distribution.

//! The `Executor` trait and its implementations.
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;

/// The `Executor` trait allows for spawning and executing fibers.
pub trait Executor: Sized {
    /// The handle type of the executor.
    type Handle: Spawn + Clone + Send + 'static;

    /// Returns the handle of the executor.
    fn handle(&self) -> Self::Handle;

    /// Runs one one unit of works.
    fn run_once(&mut self) -> io::Result<()>;

    /// Runs until the monitored fiber exits.
    fn run_fiber<T, E>(
        &mut self,
        monitor: Monitor<T, E>,
    ) -> io::Result<Result<T, MonitorError<E>>> {
        self.run_future(monitor)
    }

    /// Runs until the future is ready.
    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()?;
        }
    }

    /// Runs infinitely until an error happens.
    fn run(mut self) -> io::Result<()> {
        loop {
            self.run_once()?
        }
    }
}