salvo_utils/rt/
tokio_executor.rs

1use hyper::rt::Executor;
2use std::future::Future;
3
4/// Future executor that utilises `tokio` threads.
5#[non_exhaustive]
6#[derive(Default, Debug, Clone)]
7pub struct TokioExecutor {}
8
9impl<Fut> Executor<Fut> for TokioExecutor
10where
11    Fut: Future + Send + 'static,
12    Fut::Output: Send + 'static,
13{
14    fn execute(&self, fut: Fut) {
15        tokio::spawn(fut);
16    }
17}
18
19impl TokioExecutor {
20    /// Create new executor that relies on [`tokio::spawn`] to execute futures.
21    pub fn new() -> Self {
22        Self {}
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use crate::rt::tokio_executor::TokioExecutor;
29    use hyper::rt::Executor;
30    use tokio::sync::oneshot;
31
32    #[cfg(not(miri))]
33    #[tokio::test]
34    async fn simple_execute() -> Result<(), Box<dyn std::error::Error>> {
35        let (tx, rx) = oneshot::channel();
36        let executor = TokioExecutor::new();
37        executor.execute(async move {
38            tx.send(()).unwrap();
39        });
40        rx.await.map_err(Into::into)
41    }
42}