qubit_function/tasks/callable.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! # Callable Types
10//!
11//! Provides fallible, reusable, zero-argument computations.
12//!
13//! A `Callable<R, E>` is equivalent to `FnMut() -> Result<R, E>`, but uses
14//! task-oriented vocabulary. Use it when the operation is a computation or task
15//! whose success value matters. Use `Runnable<E>` when the operation only needs
16//! to report success or failure.
17//!
18//! The trait itself does not require `Send`; concurrent executors should add
19//! `+ Send + 'static` at their API boundary.
20//!
21//! # Author
22//!
23//! Haixing Hu
24
25use std::cell::RefCell;
26use std::rc::Rc;
27use std::sync::Arc;
28
29use parking_lot::Mutex;
30
31use crate::{
32 functions::macros::impl_function_debug_display,
33 macros::{
34 impl_arc_conversions,
35 impl_box_conversions,
36 impl_closure_trait,
37 impl_common_name_methods,
38 impl_common_new_methods,
39 impl_rc_conversions,
40 },
41 suppliers::supplier::Supplier,
42 tasks::callable_once::BoxCallableOnce,
43 tasks::runnable::BoxRunnable,
44};
45
46mod box_callable;
47pub use box_callable::BoxCallable;
48mod rc_callable;
49pub use rc_callable::RcCallable;
50mod arc_callable;
51pub use arc_callable::ArcCallable;
52
53// ============================================================================
54// Callable Trait
55// ============================================================================
56
57/// A fallible, reusable zero-argument computation.
58///
59/// Conceptually this is the same shape as `FnMut() -> Result<R, E>`: `call` takes
60/// `&mut self` and returns `Result<R, E>`, but the API uses task-oriented naming
61/// and helpers. In this crate it aligns with [`Supplier`] of `Result<R, E>`—a
62/// fallible supplier—while emphasizing executable work rather than plain value
63/// production.
64///
65/// Choose **`Callable`** when callers need the success value `R`. When only
66/// success or failure matters, use [`Runnable`](crate::tasks::Runnable), whose
67/// success type is `()`.
68///
69/// # Type Parameters
70///
71/// * `R` - The success value returned by the computation.
72/// * `E` - The error value returned when the computation fails.
73///
74/// # Examples
75///
76/// ```rust
77/// use qubit_function::Callable;
78///
79/// let mut task = || Ok::<i32, String>(21 * 2);
80/// assert_eq!(task.call().expect("call should succeed"), 42);
81/// ```
82///
83/// # Author
84///
85/// Haixing Hu
86pub trait Callable<R, E> {
87 /// Executes the computation, borrowing `self` mutably.
88 ///
89 /// # Returns
90 ///
91 /// Returns `Ok(R)` when the computation succeeds, or `Err(E)` when it
92 /// fails. The exact error meaning is defined by the concrete callable.
93 fn call(&mut self) -> Result<R, E>;
94
95 /// Converts this callable into a boxed callable.
96 ///
97 /// # Returns
98 ///
99 /// A `BoxCallable<R, E>` that executes this callable when `call()` is
100 /// invoked.
101 fn into_box(mut self) -> BoxCallable<R, E>
102 where
103 Self: Sized + 'static,
104 {
105 BoxCallable::new(move || self.call())
106 }
107
108 /// Converts this callable into an `Rc` callable.
109 ///
110 /// # Returns
111 ///
112 /// A `RcCallable<R, E>`.
113 fn into_rc(mut self) -> RcCallable<R, E>
114 where
115 Self: Sized + 'static,
116 {
117 RcCallable::new(move || self.call())
118 }
119
120 /// Converts this callable into an `Arc` callable.
121 ///
122 /// # Returns
123 ///
124 /// An `ArcCallable<R, E>`.
125 fn into_arc(mut self) -> ArcCallable<R, E>
126 where
127 Self: Sized + Send + 'static,
128 {
129 ArcCallable::new(move || self.call())
130 }
131
132 /// Converts this callable into a mutable closure.
133 ///
134 /// # Returns
135 ///
136 /// A closure implementing `FnMut() -> Result<R, E>`.
137 fn into_fn(mut self) -> impl FnMut() -> Result<R, E>
138 where
139 Self: Sized + 'static,
140 {
141 move || self.call()
142 }
143
144 /// Converts this callable into a boxed callable without consuming `self`.
145 ///
146 /// The method clones `self` and boxes the clone. Use this for cloneable
147 /// callable values that need to be reused after boxing.
148 ///
149 /// # Returns
150 ///
151 /// A new `BoxCallable<R, E>` built from a clone of this callable.
152 fn to_box(&self) -> BoxCallable<R, E>
153 where
154 Self: Clone + Sized + 'static,
155 {
156 self.clone().into_box()
157 }
158
159 /// Converts this callable into an `Rc` callable without consuming `self`.
160 ///
161 /// The method clones `self` and wraps the clone.
162 ///
163 /// # Returns
164 ///
165 /// A `RcCallable<R, E>`.
166 fn to_rc(&self) -> RcCallable<R, E>
167 where
168 Self: Clone + Sized + 'static,
169 {
170 self.clone().into_rc()
171 }
172
173 /// Converts this callable into an `Arc` callable without consuming `self`.
174 ///
175 /// The method clones `self` and wraps the clone.
176 ///
177 /// # Returns
178 ///
179 /// An `ArcCallable<R, E>`.
180 fn to_arc(&self) -> ArcCallable<R, E>
181 where
182 Self: Clone + Send + Sized + 'static,
183 {
184 self.clone().into_arc()
185 }
186
187 /// Converts this callable into a mutable closure without consuming `self`.
188 ///
189 /// The method clones `self` and returns a closure that executes the clone
190 /// on each call.
191 ///
192 /// # Returns
193 ///
194 /// A closure implementing `FnMut() -> Result<R, E>`.
195 fn to_fn(&self) -> impl FnMut() -> Result<R, E>
196 where
197 Self: Clone + Sized + 'static,
198 {
199 self.clone().into_fn()
200 }
201
202 /// Converts this callable into a one-time callable.
203 ///
204 /// The returned callable consumes itself on each invocation.
205 ///
206 /// # Returns
207 ///
208 /// A `BoxCallableOnce<R, E>`.
209 fn into_once(mut self) -> BoxCallableOnce<R, E>
210 where
211 Self: Sized + 'static,
212 {
213 BoxCallableOnce::new(move || self.call())
214 }
215
216 /// Converts this callable into a one-time callable without consuming
217 /// `self`.
218 ///
219 /// The method clones `self` and returns a one-time callable.
220 ///
221 /// # Returns
222 ///
223 /// A `BoxCallableOnce<R, E>`.
224 fn to_once(&self) -> BoxCallableOnce<R, E>
225 where
226 Self: Clone + Sized + 'static,
227 {
228 self.clone().into_once()
229 }
230
231 /// Converts this callable into a runnable by discarding the success value.
232 ///
233 /// The returned runnable preserves errors and maps any `Ok(R)` to
234 /// `Ok(())`.
235 ///
236 /// # Returns
237 ///
238 /// A `BoxRunnable<E>` that executes this callable and discards its success
239 /// value.
240 fn into_runnable(mut self) -> BoxRunnable<E>
241 where
242 Self: Sized + 'static,
243 {
244 BoxRunnable::new(move || self.call().map(|_| ()))
245 }
246}