qubit_function/tasks/callable_with/
box_callable_with.rs1#![allow(unused_imports)]
12
13use super::*;
14
15pub struct BoxCallableWith<T, R, E> {
24 pub(super) function: Box<dyn FnMut(&mut T) -> Result<R, E>>,
26 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 #[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 #[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 #[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 #[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 #[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}