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
use futures::Future;

/// An Executor that is used to spawn futures
pub trait Executor: Clone {
    /// Spawns a new asynchronous task
    fn spawn(&self, future: impl Future<Output = ()> + Send + 'static);
}

/// An Executor that uses the tokio runtime
#[derive(Clone, Debug, Default)]
pub struct TokioExecutor;

impl TokioExecutor {
    /// A new tokio executor
    pub fn new() -> Self {
        Self
    }
}

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

/// An Executor that uses the async-std runtime
#[derive(Clone, Debug)]
#[cfg(feature = "async-std-comp")]
pub struct AsyncStdExecutor;

#[cfg(feature = "async-std-comp")]
impl AsyncStdExecutor {
    /// A new async-std executor
    pub fn new() -> Self {
        Self
    }
}

#[cfg(feature = "async-std-comp")]
impl Executor for AsyncStdExecutor {
    fn spawn(&self, fut: impl Future<Output = ()> + Send + 'static) {
        async_std::task::spawn(async { fut.await });
    }
}