summaryrefslogtreecommitdiffstats
path: root/futures/src/runtime/executor.rs
blob: 855aa105948fd7e47d186fc01b045c5cdf069d40 (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
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()
    }
}

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)
    }
}