executor.rs

 1use std::{future::Future, time::Duration};
 2
 3#[cfg(test)]
 4use gpui::BackgroundExecutor;
 5
 6#[derive(Clone)]
 7pub enum Executor {
 8    Production,
 9    #[cfg(test)]
10    Deterministic(BackgroundExecutor),
11}
12
13impl Executor {
14    pub fn spawn_detached<F>(&self, future: F)
15    where
16        F: 'static + Send + Future<Output = ()>,
17    {
18        match self {
19            Executor::Production => {
20                tokio::spawn(future);
21            }
22            #[cfg(test)]
23            Executor::Deterministic(background) => {
24                background.spawn(future).detach();
25            }
26        }
27    }
28
29    pub fn sleep(&self, duration: Duration) -> impl Future<Output = ()> + use<> {
30        let this = self.clone();
31        async move {
32            match this {
33                Executor::Production => tokio::time::sleep(duration).await,
34                #[cfg(test)]
35                Executor::Deterministic(background) => background.timer(duration).await,
36            }
37        }
38    }
39}