qubit_executor/executor/direct_executor.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9use qubit_function::Callable;
10
11use super::Executor;
12
13/// Executes tasks immediately on the caller thread.
14///
15/// This executor is useful for deterministic tests and simple composition
16/// where task execution should happen in the same call stack.
17#[derive(Debug, Default, Clone, Copy)]
18pub struct DirectExecutor;
19
20impl Executor for DirectExecutor {
21 type Execution<R, E>
22 = Result<R, E>
23 where
24 R: Send + 'static,
25 E: std::fmt::Display + Send + 'static;
26
27 /// Executes the callable inline and returns its result.
28 ///
29 /// # Parameters
30 ///
31 /// * `task` - Callable to run on the caller thread.
32 ///
33 /// # Returns
34 ///
35 /// The exact `Result<R, E>` returned by the callable.
36 #[inline]
37 fn call<C, R, E>(&self, mut task: C) -> Self::Execution<R, E>
38 where
39 C: Callable<R, E> + Send + 'static,
40 R: Send + 'static,
41 E: std::fmt::Display + Send + 'static,
42 {
43 task.call()
44 }
45}