qubit_function/tasks/runnable_with/
box_runnable_with.rs1use crate::{
14 macros::{
15 impl_box_conversions,
16 impl_common_name_methods,
17 impl_common_new_methods,
18 },
19 tasks::{
20 callable_with::BoxCallableWith,
21 runnable_with::{
22 RcRunnableWith,
23 RunnableWith,
24 },
25 },
26};
27
28type BoxRunnableWithFn<T, E> = Box<dyn FnMut(&mut T) -> Result<(), E>>;
29
30pub struct BoxRunnableWith<T, E> {
36 pub(super) function: BoxRunnableWithFn<T, E>,
38 pub(super) name: Option<String>,
40}
41
42impl<T, E> BoxRunnableWith<T, E> {
43 impl_common_new_methods!(
44 (FnMut(&mut T) -> Result<(), E> + 'static),
45 |function| Box::new(function),
46 "runnable-with"
47 );
48
49 impl_common_name_methods!("runnable-with");
50
51 #[inline]
61 pub fn and_then<N>(self, next: N) -> BoxRunnableWith<T, E>
62 where
63 N: RunnableWith<T, E> + 'static,
64 T: 'static,
65 E: 'static,
66 {
67 let name = self.name;
68 let mut function = self.function;
69 let mut next = next;
70 BoxRunnableWith::new_with_optional_name(
71 move |input| {
72 function(input)?;
73 next.run_with(input)
74 },
75 name,
76 )
77 }
78
79 #[inline]
91 pub fn then_callable_with<R, C>(self, callable: C) -> BoxCallableWith<T, R, E>
92 where
93 C: crate::tasks::callable_with::CallableWith<T, R, E> + 'static,
94 T: 'static,
95 R: 'static,
96 E: 'static,
97 {
98 let name = self.name;
99 let mut function = self.function;
100 let mut callable = callable;
101 BoxCallableWith::new_with_optional_name(
102 move |input| {
103 function(input)?;
104 callable.call_with(input)
105 },
106 name,
107 )
108 }
109}
110
111impl<T, E> RunnableWith<T, E> for BoxRunnableWith<T, E> {
112 #[inline]
114 fn run_with(&mut self, input: &mut T) -> Result<(), E> {
115 (self.function)(input)
116 }
117
118 impl_box_conversions!(
119 BoxRunnableWith<T, E>,
120 RcRunnableWith,
121 FnMut(&mut T) -> Result<(), E>
122 );
123
124 #[inline]
127 fn into_callable_with(self) -> BoxCallableWith<T, (), E>
128 where
129 Self: Sized + 'static,
130 {
131 let name = self.name;
132 let mut function = self.function;
133 BoxCallableWith::new_with_optional_name(
134 move |input| {
135 function(input)?;
136 Ok(())
137 },
138 name,
139 )
140 }
141}