Skip to main content

zrx_scheduler/scheduler/step/effect/
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::marker::PhantomData;
30
31use crate::scheduler::engine::Tag;
32use crate::scheduler::step::{Result, Steps};
33
34mod builder;
35
36pub use builder::Builder;
37
38// ----------------------------------------------------------------------------
39// Traits
40// ----------------------------------------------------------------------------
41
42/// Task function.
43trait TaskFn<I>: Send {
44    /// Executes the task function.
45    ///
46    /// # Errors
47    ///
48    /// Returns [`Error`][] if the function fails to execute.
49    ///
50    /// [`Error`]: crate::scheduler::action::Error
51    fn execute(self: Box<Self>) -> Result<Steps<I>>;
52}
53
54// ----------------------------------------------------------------------------
55// Structs
56// ----------------------------------------------------------------------------
57
58/// Task.
59///
60/// Tasks are intended to be run by an [`Executor`][], providing a convenient
61/// way to encapsulate functionality that doesn't require shared state, moving
62/// it to a worker thread for parallel execution. Tasks can capture variables
63/// from their environment, and are designed to be executed once.
64///
65/// The given parameter `C` captures a specific type used during scheduling, in
66/// order to pass type information through a chain of effects inside of actions.
67/// When registering actions, the scheduler will erase the type information, as
68/// it's only necessary during compile time, not during execution.
69///
70/// [`Executor`]: zrx_executor::Executor
71pub struct Task<I, C = ()> {
72    /// Task function.
73    function: Box<dyn TaskFn<I>>,
74    /// Capture types.
75    marker: PhantomData<C>,
76}
77
78// ----------------------------------------------------------------------------
79// Implementations
80// ----------------------------------------------------------------------------
81
82impl<I, C> Task<I, C> {
83    /// Executes the task.
84    ///
85    /// # Errors
86    ///
87    /// Errors returned by the task are forwarded. Note that panics are not
88    /// caught, as this should happen on higher levels for better control.
89    #[inline]
90    pub fn execute(self) -> Result<Steps<I, C>> {
91        self.function.execute().map(Steps::tag)
92    }
93}
94
95// ----------------------------------------------------------------------------
96// Trait implementations
97// ----------------------------------------------------------------------------
98
99impl<I, C> Tag<I, C> for Task<I, C> {
100    type Target<T> = Task<I, T>;
101
102    /// Tags the task with the given type.
103    #[inline]
104    fn tag<T>(self) -> Self::Target<T> {
105        Task {
106            function: self.function,
107            marker: PhantomData,
108        }
109    }
110}
111
112// ----------------------------------------------------------------------------
113
114impl<I, C> Debug for Task<I, C> {
115    /// Formats the task for debugging.
116    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117        let function = "Box<dyn TaskFn>";
118        f.debug_struct("Task")
119            .field("function", &function)
120            .field("marker", &self.marker)
121            .finish()
122    }
123}
124
125// ----------------------------------------------------------------------------
126// Blanket implementations
127// ----------------------------------------------------------------------------
128
129impl<F, I, C> TaskFn<I> for (F, PhantomData<C>)
130where
131    F: FnOnce() -> Result<Steps<I, C>> + Send,
132    C: Send,
133{
134    #[inline]
135    fn execute(self: Box<Self>) -> Result<Steps<I>> {
136        let (f, _) = *self;
137        f().map(Steps::tag)
138    }
139}