Skip to main content

qubit_function/tasks/callable_once/
box_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// qubit-style: allow explicit-imports
11//! Defines the `BoxCallableOnce` public type.
12
13#![allow(unused_imports)]
14
15use super::*;
16
17// ============================================================================
18// BoxCallableOnce
19// ============================================================================
20
21/// Box-based one-time callable.
22///
23/// `BoxCallableOnce<R, E>` stores a
24/// `Box<dyn FnOnce() -> Result<R, E> + Send>` and can be executed only once.
25/// It is the boxed concrete implementation of [`CallableOnce`] for task
26/// objects that may be moved across threads.
27///
28/// # Type Parameters
29///
30/// * `R` - The success value returned by the computation.
31/// * `E` - The error value returned when the computation fails.
32///
33/// # Examples
34///
35/// ```rust
36/// use qubit_function::{BoxCallableOnce, CallableOnce};
37///
38/// let task = BoxCallableOnce::new(|| Ok::<i32, String>(42));
39/// assert_eq!(task.call(), Ok(42));
40/// ```
41///
42pub struct BoxCallableOnce<R, E> {
43    /// The one-time closure executed by this callable.
44    pub(super) function: Box<dyn FnOnce() -> Result<R, E> + Send>,
45    /// The optional name of this callable.
46    pub(super) name: Option<String>,
47}
48
49impl<R, E> BoxCallableOnce<R, E> {
50    impl_common_new_methods!(
51        (FnOnce() -> Result<R, E> + Send + 'static),
52        |function| Box::new(function),
53        "callable"
54    );
55
56    /// Creates a boxed callable from a one-time supplier.
57    ///
58    /// This is an explicit bridge from `SupplierOnce<Result<R, E>>` to
59    /// `CallableOnce<R, E>`.
60    ///
61    /// # Parameters
62    ///
63    /// * `supplier` - The supplier that produces the callable result.
64    ///
65    /// # Returns
66    ///
67    /// A new `BoxCallableOnce<R, E>`.
68    #[inline]
69    pub fn from_supplier<S>(supplier: S) -> Self
70    where
71        S: SupplierOnce<Result<R, E>> + Send + 'static,
72    {
73        Self::new(move || supplier.get())
74    }
75
76    impl_common_name_methods!("callable");
77
78    /// Maps the success value of this callable.
79    ///
80    /// # Parameters
81    ///
82    /// * `mapper` - Function that transforms the success value.
83    ///
84    /// # Returns
85    ///
86    /// A new callable that applies `mapper` when this callable succeeds.
87    #[inline]
88    pub fn map<U, M>(self, mapper: M) -> BoxCallableOnce<U, E>
89    where
90        M: FnOnce(R) -> U + Send + 'static,
91        R: 'static,
92        E: 'static,
93    {
94        let name = self.name;
95        let function = self.function;
96        BoxCallableOnce::new_with_optional_name(move || function().map(mapper), name)
97    }
98
99    /// Maps the error value of this callable.
100    ///
101    /// # Parameters
102    ///
103    /// * `mapper` - Function that transforms the error value.
104    ///
105    /// # Returns
106    ///
107    /// A new callable that applies `mapper` when this callable fails.
108    #[inline]
109    pub fn map_err<E2, M>(self, mapper: M) -> BoxCallableOnce<R, E2>
110    where
111        M: FnOnce(E) -> E2 + Send + 'static,
112        R: 'static,
113        E: 'static,
114    {
115        let name = self.name;
116        let function = self.function;
117        BoxCallableOnce::new_with_optional_name(move || function().map_err(mapper), name)
118    }
119
120    /// Chains another fallible computation after this callable succeeds.
121    ///
122    /// # Parameters
123    ///
124    /// * `next` - Function that receives the success value and returns the next
125    ///   result.
126    ///
127    /// # Returns
128    ///
129    /// A new callable that runs `next` only when this callable succeeds.
130    #[inline]
131    pub fn and_then<U, N>(self, next: N) -> BoxCallableOnce<U, E>
132    where
133        N: FnOnce(R) -> Result<U, E> + Send + 'static,
134        R: 'static,
135        E: 'static,
136    {
137        let name = self.name;
138        let function = self.function;
139        BoxCallableOnce::new_with_optional_name(move || function().and_then(next), name)
140    }
141}
142
143impl<R, E> CallableOnce<R, E> for BoxCallableOnce<R, E> {
144    /// Executes the boxed callable.
145    #[inline]
146    fn call(self) -> Result<R, E> {
147        (self.function)()
148    }
149
150    impl_box_once_conversions!(BoxCallableOnce<R, E>, CallableOnce, FnOnce() -> Result<R, E>);
151
152    /// Converts this boxed callable into a boxed runnable while preserving its
153    /// name.
154    #[inline]
155    fn into_runnable(self) -> BoxRunnableOnce<E>
156    where
157        Self: Sized + 'static,
158    {
159        let name = self.name;
160        let function = self.function;
161        BoxRunnableOnce::new_with_optional_name(move || function().map(|_| ()), name)
162    }
163
164    /// Converts this boxed callable into a local boxed callable while
165    /// preserving its name.
166    #[inline]
167    fn into_local_box(self) -> LocalBoxCallableOnce<R, E>
168    where
169        Self: Sized + 'static,
170    {
171        let name = self.name;
172        let function = self.function;
173        LocalBoxCallableOnce::new_with_optional_name(function, name)
174    }
175
176    /// Converts this boxed callable into a local boxed runnable while
177    /// preserving its name.
178    #[inline]
179    fn into_local_runnable(self) -> LocalBoxRunnableOnce<E>
180    where
181        Self: Sized + 'static,
182    {
183        let name = self.name;
184        let function = self.function;
185        LocalBoxRunnableOnce::new_with_optional_name(move || function().map(|_| ()), name)
186    }
187}
188
189impl<R, E> SupplierOnce<Result<R, E>> for BoxCallableOnce<R, E> {
190    /// Executes the boxed callable as a one-time supplier of `Result<R, E>`.
191    #[inline]
192    fn get(self) -> Result<R, E> {
193        self.call()
194    }
195}
196
197impl<F, R, E> CallableOnce<R, E> for F
198where
199    F: FnOnce() -> Result<R, E>,
200{
201    /// Executes the closure as a one-time callable.
202    #[inline]
203    fn call(self) -> Result<R, E> {
204        self()
205    }
206}
207
208impl_function_debug_display!(BoxCallableOnce<R, E>);