Skip to main content

moirai_core/task/
traits.rs

1use super::id_and_context::TaskContext;
2
3/// The core trait for executable tasks in the Moirai runtime.
4pub trait Task: Send + 'static {
5    /// The output type produced by this task.
6    type Output: Send + 'static;
7
8    /// Execute this task to completion.
9    fn execute(self) -> Self::Output;
10
11    /// Get the task context for scheduling and debugging.
12    fn context(&self) -> &TaskContext;
13
14    /// Check if this task can be stolen by another thread.
15    fn is_stealable(&self) -> bool {
16        true
17    }
18
19    /// Estimate the computational cost of this task (for load balancing).
20    fn estimated_cost(&self) -> u32 {
21        1
22    }
23}
24
25#[cfg(feature = "std")]
26impl<T> Task for Box<T>
27where
28    T: Task,
29{
30    type Output = T::Output;
31
32    fn execute(self) -> Self::Output {
33        (*self).execute()
34    }
35
36    fn context(&self) -> &TaskContext {
37        (**self).context()
38    }
39
40    fn is_stealable(&self) -> bool {
41        (**self).is_stealable()
42    }
43
44    fn estimated_cost(&self) -> u32 {
45        (**self).estimated_cost()
46    }
47}