Skip to main content

recoverable_spawn/common/
trait.rs

1use super::*;
2
3/// Trait alias for functions that can be executed in a recoverable context.
4///
5/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
6pub trait FunctionOnceTrait: FnOnce() + Send + Sync + 'static {}
7
8impl<T> FunctionOnceTrait for T where T: FnOnce() + Send + Sync + 'static {}
9
10/// Trait alias for functions that can be executed in a recoverable context.
11///
12/// - Functions implementing this trait must satisfy `Fn() + Send + Sync + 'static`.
13pub trait FunctionTrait: Fn() + Send + Sync + 'static {}
14
15impl<T> FunctionTrait for T where T: Fn() + Send + Sync + 'static {}
16
17/// Trait alias for functions that can be executed in a recoverable context.
18///
19/// - Functions implementing this trait must satisfy `FnMut() + Send + Sync + 'static`.
20pub trait FunctionMutTrait: FnMut() + Send + Sync + 'static {}
21
22impl<T> FunctionMutTrait for T where T: FnMut() + Send + Sync + 'static {}
23
24/// Trait alias for asynchronous functions that can be executed in a recoverable context.
25///
26/// # Arguments
27///
28/// - `FnOnce() -> Future` - Function that returns a Future.
29///
30/// # Returns
31///
32/// - `Future` - The asynchronous computation result.
33pub trait AsyncRecoverableFunction: Send + Sync + 'static {
34    type Output: Send;
35    type Future: Future<Output = Self::Output> + Send;
36
37    /// Executes the asynchronous function.
38    ///
39    /// # Returns
40    ///
41    /// - `Self::Future` - The future that resolves once the function completes.
42    fn call(self) -> Self::Future;
43}
44
45impl<F, Fut, O> AsyncRecoverableFunction for F
46where
47    F: FnOnce() -> Fut + Send + Sync + 'static,
48    Fut: Future<Output = O> + Send + 'static,
49    O: Send + 'static,
50{
51    type Output = O;
52    type Future = Fut;
53
54    /// Calls the underlying function to obtain its future.
55    ///
56    /// # Returns
57    ///
58    /// - `Self::Future` - The future returned by the wrapped function.
59    fn call(self) -> Self::Future {
60        self()
61    }
62}
63
64/// Trait alias for asynchronous error-handling functions used in a recoverable context.
65///
66/// # Arguments
67///
68/// - `Arc<String>` - The error message to handle.
69///
70/// # Returns
71///
72/// - `Future` - The asynchronous error handling result.
73pub trait AsyncErrorHandlerFunction: Send + Sync + 'static {
74    type Future: Future<Output = ()> + Send;
75
76    /// Handles an error asynchronously.
77    ///
78    /// # Arguments
79    ///
80    /// - `Arc<String>` - The error message to handle.
81    ///
82    /// # Returns
83    ///
84    /// - `Self::Future` - The future that resolves once the error has been handled.
85    fn call(self, error: Arc<String>) -> Self::Future;
86}
87
88impl<F, Fut> AsyncErrorHandlerFunction for F
89where
90    F: FnOnce(Arc<String>) -> Fut + Send + Sync + 'static,
91    Fut: Future<Output = ()> + Send + 'static,
92{
93    type Future = Fut;
94
95    /// Calls the underlying error handler to obtain its future.
96    ///
97    /// # Arguments
98    ///
99    /// - `Arc<String>` - The error message to handle.
100    ///
101    /// # Returns
102    ///
103    /// - `Self::Future` - The future returned by the wrapped error handler.
104    fn call(self, error: Arc<String>) -> Self::Future {
105        self(error)
106    }
107}
108
109/// Trait alias for functions that can be executed in a recoverable context.
110///
111/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
112pub trait RecoverableFunction: FnOnce() + Send + Sync + 'static {}
113
114impl<T> RecoverableFunction for T where T: FnOnce() + Send + Sync + 'static {}
115
116/// Trait alias for error-handling functions used in a recoverable context.
117///
118/// # Arguments
119///
120/// - `&str` - The error message to handle.
121pub trait ErrorHandlerFunction: FnOnce(&str) + Send + Sync + 'static {}
122
123impl<T> ErrorHandlerFunction for T where T: FnOnce(&str) + Send + Sync + 'static {}