recoverable_spawn/thread/
trait.rs

1use std::{future::Future, sync::Arc};
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    fn call(self) -> Self::Future;
39}
40
41impl<F, Fut, O> AsyncRecoverableFunction for F
42where
43    F: FnOnce() -> Fut + Send + Sync + 'static,
44    Fut: Future<Output = O> + Send + 'static,
45    O: Send + 'static,
46{
47    type Output = O;
48    type Future = Fut;
49
50    fn call(self) -> Self::Future {
51        self()
52    }
53}
54
55/// Trait alias for asynchronous error-handling functions used in a recoverable context.
56///
57/// # Arguments
58///
59/// - `Arc<String>` - The error message to handle.
60///
61/// # Returns
62///
63/// - `Future` - The asynchronous error handling result.
64pub trait AsyncErrorHandlerFunction: Send + Sync + 'static {
65    type Future: Future<Output = ()> + Send;
66
67    /// Handles an error asynchronously.
68    ///
69    /// - `error`: The error message to handle.
70    fn call(self, error: Arc<String>) -> Self::Future;
71}
72
73impl<F, Fut> AsyncErrorHandlerFunction for F
74where
75    F: FnOnce(Arc<String>) -> Fut + Send + Sync + 'static,
76    Fut: Future<Output = ()> + Send + 'static,
77{
78    type Future = Fut;
79
80    fn call(self, error: Arc<String>) -> Self::Future {
81        self(error)
82    }
83}
84
85/// Trait alias for functions that can be executed in a recoverable context.
86///
87/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
88pub trait RecoverableFunction: FnOnce() + Send + Sync + 'static {}
89
90impl<T> RecoverableFunction for T where T: FnOnce() + Send + Sync + 'static {}
91
92/// Trait alias for error-handling functions used in a recoverable context.
93///
94/// # Arguments
95///
96/// - `&str` - The error message to handle.
97pub trait ErrorHandlerFunction: FnOnce(&str) + Send + Sync + 'static {}
98
99impl<T> ErrorHandlerFunction for T where T: FnOnce(&str) + Send + Sync + 'static {}