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
use std::fmt;

use void::Void;

use futures::future::Future;
use futures_cpupool::CpuPool;
use tokio_core::reactor;

pub trait Executor {
    fn execute(&self, f: Box<Future<Item=(), Error=Void> + Send + 'static>);
}

impl Executor for CpuPool {
    fn execute(&self, f: Box<Future<Item=(), Error=Void> + Send + 'static>) {
        self.spawn(f).forget();
    }
}

impl Executor for reactor::Handle {
    fn execute(&self, f: Box<Future<Item=(), Error=Void> + Send + 'static>) {
        self.spawn(f.map_err(|e| match e {}));
    }
}

// Where execute requests on client and responses on server
#[derive(Clone)]
pub enum CpuPoolOption {
    // Execute in event loop
    SingleThread,
    // Execute in provided CpuPool
    CpuPool(CpuPool),
}

impl fmt::Debug for CpuPoolOption {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &CpuPoolOption::SingleThread => write!(f, "SingleThread"),
            &CpuPoolOption::CpuPool(..) => write!(f, "CpuPool"),
        }
    }
}

impl CpuPoolOption {
    pub(crate) fn make_executor(&self, lh: &reactor::Handle) -> Box<Executor> {
        match self {
            &CpuPoolOption::SingleThread => Box::new(lh.clone()),
            &CpuPoolOption::CpuPool(ref pool) => Box::new(pool.clone()),
        }
    }
}