Skip to main content

qubit_function/tasks/
callable_once.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Callable Once Types
11//!
12//! Provides fallible, one-time, zero-argument computations.
13//!
14//! A `CallableOnce<R, E>` is equivalent to `FnOnce() -> Result<R, E>`, but uses
15//! task-oriented vocabulary. Use it when the operation is a computation or task
16//! whose success value matters. Use `RunnableOnce<E>` when the operation only
17//! needs to report success or failure.
18//!
19//! The trait itself does not require `Send`; concurrent executors should add
20//! `+ Send + 'static` at their API boundary.
21//!
22
23use crate::{
24    functions::macros::impl_function_debug_display,
25    macros::{
26        impl_box_once_conversions,
27        impl_common_name_methods,
28        impl_common_new_methods,
29    },
30    suppliers::supplier_once::SupplierOnce,
31    tasks::runnable_once::{
32        BoxRunnableOnce,
33        LocalBoxRunnableOnce,
34    },
35};
36
37mod box_callable_once;
38pub use box_callable_once::BoxCallableOnce;
39mod local_box_callable_once;
40pub use local_box_callable_once::LocalBoxCallableOnce;
41
42// ============================================================================
43// CallableOnce Trait
44// ============================================================================
45
46/// A fallible one-time computation.
47///
48/// Conceptually this matches `FnOnce() -> Result<R, E>`: `call` consumes `self`
49/// and returns `Result<R, E>`, but the surface uses task-oriented naming and
50/// helpers instead of closure types. It is a semantic specialization of
51/// `SupplierOnce<Result<R, E>>` for executable computations and deferred tasks.
52///
53/// Choose **`CallableOnce`** when callers need the success value `R`. When only
54/// success or failure matters, use [`RunnableOnce`](crate::tasks::runnable_once::RunnableOnce),
55/// whose success type is `()`.
56///
57/// # Type Parameters
58///
59/// * `R` - The success value returned by the computation.
60/// * `E` - The error value returned when the computation fails.
61///
62/// # Examples
63///
64/// ```rust
65/// use qubit_function::{CallableOnce, BoxCallableOnce};
66///
67/// let task = || Ok::<i32, String>(21 * 2);
68/// assert_eq!(task.call(), Ok(42));
69/// ```
70///
71pub trait CallableOnce<R, E> {
72    /// Executes the computation, consuming `self`.
73    ///
74    /// # Returns
75    ///
76    /// Returns `Ok(R)` when the computation succeeds, or `Err(E)` when it
77    /// fails. The exact error meaning is defined by the concrete callable.
78    fn call(self) -> Result<R, E>;
79
80    /// Converts this callable into a boxed callable.
81    ///
82    /// # Returns
83    ///
84    /// A `BoxCallableOnce<R, E>` that executes this callable when `call()` is
85    /// invoked.
86    fn into_box(self) -> BoxCallableOnce<R, E>
87    where
88        Self: Sized + Send + 'static,
89    {
90        BoxCallableOnce::new(move || self.call())
91    }
92
93    /// Converts this callable into a local boxed callable.
94    ///
95    /// # Returns
96    ///
97    /// A `LocalBoxCallableOnce<R, E>` that may hold non-`Send` captures and
98    /// must be executed on the local thread.
99    fn into_local_box(self) -> LocalBoxCallableOnce<R, E>
100    where
101        Self: Sized + 'static,
102    {
103        LocalBoxCallableOnce::new(move || self.call())
104    }
105
106    /// Converts this callable into a closure.
107    ///
108    /// # Returns
109    ///
110    /// A closure implementing `FnOnce() -> Result<R, E>`.
111    fn into_fn(self) -> impl FnOnce() -> Result<R, E>
112    where
113        Self: Sized + 'static,
114    {
115        move || self.call()
116    }
117
118    /// Converts this callable into a boxed callable without consuming `self`.
119    ///
120    /// The method clones `self` and boxes the clone. Use this for cloneable
121    /// callable values that need to be reused after boxing.
122    ///
123    /// # Returns
124    ///
125    /// A new `BoxCallableOnce<R, E>` built from a clone of this callable.
126    fn to_box(&self) -> BoxCallableOnce<R, E>
127    where
128        Self: Clone + Send + Sized + 'static,
129    {
130        self.clone().into_box()
131    }
132
133    /// Converts this callable into a local boxed callable without consuming
134    /// `self`.
135    ///
136    /// The method clones `self` and boxes the clone without requiring `Send`.
137    ///
138    /// # Returns
139    ///
140    /// A new `LocalBoxCallableOnce<R, E>` built from a clone of this callable.
141    fn to_local_box(&self) -> LocalBoxCallableOnce<R, E>
142    where
143        Self: Clone + Sized + 'static,
144    {
145        self.clone().into_local_box()
146    }
147
148    /// Converts this callable into a closure without consuming `self`.
149    ///
150    /// The method clones `self` and returns a one-time closure that executes
151    /// the clone.
152    ///
153    /// # Returns
154    ///
155    /// A closure implementing `FnOnce() -> Result<R, E>`.
156    fn to_fn(&self) -> impl FnOnce() -> Result<R, E>
157    where
158        Self: Clone + Sized + 'static,
159    {
160        self.clone().into_fn()
161    }
162
163    /// Converts this callable into a runnable by discarding the success value.
164    ///
165    /// The returned runnable preserves errors and maps any `Ok(R)` to
166    /// `Ok(())`.
167    ///
168    /// # Returns
169    ///
170    /// A `BoxRunnableOnce<E>` that executes this callable and discards its
171    /// success value.
172    fn into_runnable(self) -> BoxRunnableOnce<E>
173    where
174        Self: Sized + Send + 'static,
175    {
176        BoxRunnableOnce::new(move || self.call().map(|_| ()))
177    }
178
179    /// Converts this callable into a local runnable by discarding the success
180    /// value.
181    ///
182    /// # Returns
183    ///
184    /// A `LocalBoxRunnableOnce<E>` that may hold non-`Send` captures and maps
185    /// any `Ok(R)` to `Ok(())`.
186    fn into_local_runnable(self) -> LocalBoxRunnableOnce<E>
187    where
188        Self: Sized + 'static,
189    {
190        LocalBoxRunnableOnce::new(move || self.call().map(|_| ()))
191    }
192}