vortex_layout/scan/executor/
mod.rs

1#[cfg(feature = "tokio")]
2mod tokio;
3
4mod threads;
5
6use std::future::Future;
7
8use futures::future::BoxFuture;
9pub use threads::*;
10#[cfg(feature = "tokio")]
11pub use tokio::*;
12use vortex_error::VortexResult;
13
14pub trait Executor {
15    /// Spawns a future to run on a different runtime.
16    /// The runtime will make progress on the future without being directly polled, returning its output.
17    fn spawn<F>(&self, f: F) -> BoxFuture<'static, VortexResult<F::Output>>
18    where
19        F: Future + Send + 'static,
20        <F as Future>::Output: Send + 'static;
21}
22
23/// Generic wrapper around different async runtimes. Used to spawn async tasks to run in the background, concurrently with other tasks.
24#[derive(Clone)]
25pub enum TaskExecutor {
26    Threads(ThreadsExecutor),
27    #[cfg(feature = "tokio")]
28    Tokio(TokioExecutor),
29}
30
31#[async_trait::async_trait]
32impl Executor for TaskExecutor {
33    fn spawn<F>(&self, f: F) -> BoxFuture<'static, VortexResult<F::Output>>
34    where
35        F: Future + Send + 'static,
36        <F as Future>::Output: Send + 'static,
37    {
38        match self {
39            TaskExecutor::Threads(threads_executor) => threads_executor.spawn(f),
40            #[cfg(feature = "tokio")]
41            TaskExecutor::Tokio(tokio_executor) => tokio_executor.spawn(f),
42        }
43    }
44}