dip_task/
async_action.rs

1use std::{fmt::Debug, future::Future};
2use tokio::{runtime::Runtime, sync::mpsc};
3
4pub struct AsyncActionPool<Action> {
5    runner: Runtime,
6    tx: mpsc::Sender<Action>,
7}
8
9impl<Action> AsyncActionPool<Action> {
10    pub fn new(tx: mpsc::Sender<Action>) -> Self {
11        Self {
12            runner: Runtime::new().unwrap(),
13            tx,
14        }
15    }
16
17    pub fn send<F>(&self, future: F)
18    where
19        F: Future<Output = Action> + Send + 'static,
20        F::Output: Send + 'static + Debug,
21    {
22        let tx = self.tx.clone();
23
24        self.runner.spawn(async move {
25            let task = future.await;
26            tx.send(task).await.unwrap();
27        });
28    }
29}
30
31pub type NoAsyncAction = ();