Skip to main content

zrx_executor/executor/
task.rs

1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Task.
27
28use std::fmt::{self, Debug};
29use std::panic::UnwindSafe;
30
31mod tasks;
32
33pub use tasks::Tasks;
34
35// ----------------------------------------------------------------------------
36// Traits
37// ----------------------------------------------------------------------------
38
39/// Task.
40///
41/// Tasks are units of work that can be submitted to an [`Executor`][], which
42/// forwards them for execution to an execution [`Strategy`][] that is set when
43/// creating the [`Executor`][]. Moreover, tasks can create and return further
44/// [`Tasks`], which are handled by the same execution strategy, allowing for
45/// immediate or deferred execution, as implemented by the strategy. If a task
46/// panics, it doesn't take the worker thread or executor with it.
47///
48/// Note that tasks will almost always need to capture environment variables,
49/// which is why they're created from [`FnOnce`] and must be [`Send`].
50///
51/// [`Executor`]: crate::executor::Executor
52/// [`Strategy`]: crate::executor::strategy::Strategy
53pub trait Task: Send + UnwindSafe + 'static {
54    /// Executes the task.
55    ///
56    /// This methods executes the task, and may return further tasks as part of
57    /// a task collection, which are executed by the same execution strategy.
58    /// As task execution must be infallible, tasks might use channels in order
59    /// to communicate results or errors back to the main thread.
60    fn execute(self: Box<Self>) -> Tasks;
61}
62
63// ----------------------------------------------------------------------------
64// Trait implementations
65// ----------------------------------------------------------------------------
66
67impl<T> From<T> for Box<dyn Task>
68where
69    T: Task,
70{
71    /// Creates a boxed task from a task.
72    ///
73    /// This implementation ensures we can comfortably pass bare closures, as
74    /// well as boxed tasks to [`Executor::submit`][], which allows to resubmit
75    /// tasks that were returned as part of [`Error::Submit`][] due to capacity
76    /// limits of the execution strategy.
77    ///
78    /// [`Executor::submit`]: crate::executor::Executor::submit
79    /// [`Error::Submit`]: crate::executor::Error::Submit
80    #[inline]
81    fn from(task: T) -> Self {
82        Box::new(task)
83    }
84}
85
86// ----------------------------------------------------------------------------
87
88impl Debug for Box<dyn Task> {
89    /// Formats the task for debugging.
90    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
91        f.write_str("Box<dyn Task>")
92    }
93}
94
95// ----------------------------------------------------------------------------
96// Blanket implementations
97// ----------------------------------------------------------------------------
98
99impl<F, R> Task for F
100where
101    F: FnOnce() -> R + Send + UnwindSafe + 'static,
102    R: Into<Tasks>,
103{
104    #[inline]
105    fn execute(self: Box<Self>) -> Tasks {
106        self().into()
107    }
108}