qubit_function/tasks/callable/
arc_callable.rs1#![allow(unused_imports)]
14
15use super::*;
16
17pub struct ArcCallable<R, E> {
32 pub(super) function: Arc<Mutex<dyn FnMut() -> Result<R, E> + Send>>,
34 pub(super) name: Option<String>,
36}
37
38impl<R, E> Clone for ArcCallable<R, E> {
39 #[inline]
40 fn clone(&self) -> Self {
41 Self {
42 function: Arc::clone(&self.function),
43 name: self.name.clone(),
44 }
45 }
46}
47
48impl<R, E> ArcCallable<R, E> {
49 impl_common_new_methods!(
50 (FnMut() -> Result<R, E> + Send + 'static),
51 |function| Arc::new(Mutex::new(function)),
52 "callable"
53 );
54
55 #[inline]
65 pub fn from_supplier<S>(supplier: S) -> Self
66 where
67 S: Supplier<Result<R, E>> + Send + 'static,
68 {
69 Self::new(move || supplier.get())
70 }
71
72 impl_common_name_methods!("callable");
73}
74
75impl<R, E> Callable<R, E> for ArcCallable<R, E> {
76 #[inline]
78 fn call(&mut self) -> Result<R, E> {
79 (self.function.lock())()
80 }
81
82 impl_arc_conversions!(
83 ArcCallable<R, E>,
84 BoxCallable,
85 RcCallable,
86 BoxCallableOnce,
87 FnMut() -> Result<R, E>
88 );
89
90 #[inline]
93 fn into_local_once(self) -> LocalBoxCallableOnce<R, E>
94 where
95 Self: Sized + 'static,
96 {
97 let name = self.name;
98 let function = self.function;
99 LocalBoxCallableOnce::new_with_optional_name(move || (function.lock())(), name)
100 }
101
102 #[inline]
105 fn to_local_once(&self) -> LocalBoxCallableOnce<R, E>
106 where
107 Self: Clone + Sized + 'static,
108 {
109 self.clone().into_local_once()
110 }
111
112 #[inline]
115 fn into_runnable(self) -> BoxRunnable<E>
116 where
117 Self: Sized + 'static,
118 {
119 let name = self.name;
120 let function = self.function;
121 BoxRunnable::new_with_optional_name(move || (function.lock())().map(|_| ()), name)
122 }
123}
124
125impl_closure_trait!(
126 Callable<R, E>,
127 call,
128 FnMut() -> Result<R, E>
129);
130
131impl_function_debug_display!(BoxCallable<R, E>);
132impl_function_debug_display!(RcCallable<R, E>);
133impl_function_debug_display!(ArcCallable<R, E>);