Skip to main content

qubit_function/tasks/callable_with/
box_callable_with.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! Defines the `BoxCallableWith` public type.
10
11#![allow(unused_imports)]
12
13use super::*;
14
15/// Box-based callable with mutable input.
16///
17/// `BoxCallableWith<T, R, E>` stores a
18/// `Box<dyn FnMut(&mut T) -> Result<R, E>>` and can be called repeatedly.
19///
20/// # Author
21///
22/// Haixing Hu
23pub struct BoxCallableWith<T, R, E> {
24    /// The stateful closure executed by this callable.
25    pub(super) function: Box<dyn FnMut(&mut T) -> Result<R, E>>,
26    /// The optional name of this callable.
27    pub(super) name: Option<String>,
28}
29
30impl<T, R, E> BoxCallableWith<T, R, E> {
31    impl_common_new_methods!(
32        (FnMut(&mut T) -> Result<R, E> + 'static),
33        |function| Box::new(function),
34        "callable-with"
35    );
36
37    impl_common_name_methods!("callable-with");
38
39    /// Maps the success value of this callable.
40    ///
41    /// # Parameters
42    ///
43    /// * `mapper` - Function that transforms the success value.
44    ///
45    /// # Returns
46    ///
47    /// A new callable with mutable input that applies `mapper` on success.
48    #[inline]
49    pub fn map<U, M>(self, mut mapper: M) -> BoxCallableWith<T, U, E>
50    where
51        M: FnMut(R) -> U + 'static,
52        T: 'static,
53        R: 'static,
54        E: 'static,
55    {
56        let name = self.name;
57        let mut function = self.function;
58        BoxCallableWith::new_with_optional_name(move |input| function(input).map(&mut mapper), name)
59    }
60
61    /// Maps the error value of this callable.
62    ///
63    /// # Parameters
64    ///
65    /// * `mapper` - Function that transforms the error value.
66    ///
67    /// # Returns
68    ///
69    /// A new callable with mutable input that applies `mapper` on failure.
70    #[inline]
71    pub fn map_err<E2, M>(self, mut mapper: M) -> BoxCallableWith<T, R, E2>
72    where
73        M: FnMut(E) -> E2 + 'static,
74        T: 'static,
75        R: 'static,
76        E: 'static,
77    {
78        let name = self.name;
79        let mut function = self.function;
80        BoxCallableWith::new_with_optional_name(
81            move |input| function(input).map_err(&mut mapper),
82            name,
83        )
84    }
85
86    /// Chains another fallible computation after this callable succeeds.
87    ///
88    /// # Parameters
89    ///
90    /// * `next` - Function receiving the success value and mutable input.
91    ///
92    /// # Returns
93    ///
94    /// A new callable that runs `next` only when this callable succeeds.
95    #[inline]
96    pub fn and_then<U, N>(self, next: N) -> BoxCallableWith<T, U, E>
97    where
98        N: FnMut(R, &mut T) -> Result<U, E> + 'static,
99        T: 'static,
100        R: 'static,
101        E: 'static,
102    {
103        let name = self.name;
104        let mut function = self.function;
105        let mut next = next;
106        BoxCallableWith::new_with_optional_name(
107            move |input| {
108                let value = function(input)?;
109                next(value, input)
110            },
111            name,
112        )
113    }
114}
115
116impl<T, R, E> CallableWith<T, R, E> for BoxCallableWith<T, R, E> {
117    /// Executes the boxed callable with mutable input.
118    #[inline]
119    fn call_with(&mut self, input: &mut T) -> Result<R, E> {
120        (self.function)(input)
121    }
122
123    impl_box_conversions!(
124        BoxCallableWith<T, R, E>,
125        RcCallableWith,
126        FnMut(&mut T) -> Result<R, E>
127    );
128
129    /// Converts this boxed callable into a boxed runnable while preserving its
130    /// name.
131    #[inline]
132    fn into_runnable_with(self) -> BoxRunnableWith<T, E>
133    where
134        Self: Sized + 'static,
135    {
136        let name = self.name;
137        let mut function = self.function;
138        BoxRunnableWith::new_with_optional_name(move |input| function(input).map(|_| ()), name)
139    }
140}