Skip to main content

qubit_executor/executor/
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::{
10    Callable,
11    Runnable,
12};
13
14/// Executes fallible one-time tasks according to an implementation-defined strategy.
15///
16/// `Executor` models an execution strategy, not a managed task service. An
17/// executor may run a task immediately, retry it, delay it, schedule it on
18/// another runtime, or return a handle that represents work running elsewhere.
19/// The associated [`Self::Execution`] type describes how this executor exposes
20/// the result of a single execution.
21///
22/// # Author
23///
24/// Haixing Hu
25pub trait Executor: Send + Sync {
26    /// The result carrier returned for one execution.
27    ///
28    /// Implementations choose the carrier that matches their execution model.
29    /// For example, a direct executor can use `Result<R, E>`, while a threaded
30    /// executor can use a task handle and a future-backed executor can use a
31    /// future.
32    type Execution<R, E>
33    where
34        R: Send + 'static,
35        E: std::fmt::Display + Send + 'static;
36
37    /// Executes a runnable task and returns this executor's result carrier.
38    ///
39    /// This is the unit-returning counterpart of [`Self::call`]. The returned
40    /// carrier reports the runnable's `Result<(), E>` according to the concrete
41    /// executor's execution model.
42    ///
43    /// # Parameters
44    ///
45    /// * `task` - The fallible action to execute.
46    ///
47    /// # Returns
48    ///
49    /// The execution carrier for the submitted runnable.
50    #[inline]
51    fn execute<T, E>(&self, task: T) -> Self::Execution<(), E>
52    where
53        T: Runnable<E> + Send + 'static,
54        E: std::fmt::Display + Send + 'static,
55    {
56        let mut task = task;
57        self.call(move || task.run())
58    }
59
60    /// Executes a callable task and returns this executor's result carrier.
61    ///
62    /// # Parameters
63    ///
64    /// * `task` - The fallible computation to execute.
65    ///
66    /// # Returns
67    ///
68    /// The execution carrier for the submitted callable. Its exact behavior is
69    /// defined by the concrete executor.
70    fn call<C, R, E>(&self, task: C) -> Self::Execution<R, E>
71    where
72        C: Callable<R, E> + Send + 'static,
73        R: Send + 'static,
74        E: std::fmt::Display + Send + 'static;
75}