Skip to main content

qubit_executor/executor/
direct_executor.rs

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