executor.rs

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