summaryrefslogtreecommitdiffstats
path: root/futures/src/runtime/executor.rs
blob: eec5e231ef1a1ebf01d1aaf6afca59a9787d83f2 (plain) (blame)
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
use futures::Future;

pub trait Executor {
    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static);

    fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
        f()
    }
}

#[cfg(feature = "thread-pool")]
impl Executor for futures::executor::ThreadPool {
    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
        self.spawn_ok(future);
    }
}

#[cfg(feature = "tokio")]
impl Executor for tokio::runtime::Runtime {
    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static) {
        let _ = tokio::runtime::Runtime::spawn(self, future);
    }

    fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
        tokio::runtime::Runtime::enter(self, f)
    }
}