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/// - Functions implementing this trait must return a `Future` and satisfy
27///   `FnOnce() -> Future + Send + Sync + 'static`.
28pub trait AsyncRecoverableFunction: Send + Sync + 'static {
29    type Output: Send;
30    type Future: Future<Output = Self::Output> + Send;
31
32    /// Executes the asynchronous function.
33    fn call(self) -> Self::Future;
34}
35
36impl<F, Fut, O> AsyncRecoverableFunction for F
37where
38    F: FnOnce() -> Fut + Send + Sync + 'static,
39    Fut: Future<Output = O> + Send + 'static,
40    O: Send + 'static,
41{
42    type Output = O;
43    type Future = Fut;
44
45    fn call(self) -> Self::Future {
46        self()
47    }
48}
49
50/// Trait alias for asynchronous error-handling functions used in a recoverable context.
51///
52/// - Functions implementing this trait must accept a `Arc<String>` as an error message,
53///   return a `Future`, and satisfy `FnOnce(Arc<String>) -> Future + Send + Sync + 'static`.
54pub trait AsyncErrorHandlerFunction: Send + Sync + 'static {
55    type Future: Future<Output = ()> + Send;
56
57    /// Handles an error asynchronously.
58    ///
59    /// - `error`: The error message to handle.
60    fn call(self, error: Arc<String>) -> Self::Future;
61}
62
63impl<F, Fut> AsyncErrorHandlerFunction for F
64where
65    F: FnOnce(Arc<String>) -> Fut + Send + Sync + 'static,
66    Fut: Future<Output = ()> + Send + 'static,
67{
68    type Future = Fut;
69
70    fn call(self, error: Arc<String>) -> Self::Future {
71        self(error)
72    }
73}
74
75/// Trait alias for functions that can be executed in a recoverable context.
76///
77/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
78pub trait RecoverableFunction: FnOnce() + Send + Sync + 'static {}
79
80impl<T> RecoverableFunction for T where T: FnOnce() + Send + Sync + 'static {}
81
82/// Trait alias for error-handling functions used in a recoverable context.
83///
84/// - Functions implementing this trait must accept a `&str` as an error message
85///   and satisfy `FnOnce(&str) + Send + Sync + 'static`.
86pub trait ErrorHandlerFunction: FnOnce(&str) + Send + Sync + 'static {}
87
88impl<T> ErrorHandlerFunction for T where T: FnOnce(&str) + Send + Sync + 'static {}