recoverable_spawn/thread/trait.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
use std::{future::Future, sync::Arc};
/// Trait alias for functions that can be executed in a recoverable context.
///
/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
pub trait FunctionOnceTrait: FnOnce() + Send + Sync + 'static {}
impl<T> FunctionOnceTrait for T where T: FnOnce() + Send + Sync + 'static {}
/// Trait alias for functions that can be executed in a recoverable context.
///
/// - Functions implementing this trait must satisfy `Fn() + Send + Sync + 'static`.
pub trait FunctionTrait: Fn() + Send + Sync + 'static {}
impl<T> FunctionTrait for T where T: Fn() + Send + Sync + 'static {}
/// Trait alias for functions that can be executed in a recoverable context.
///
/// - Functions implementing this trait must satisfy `FnMut() + Send + Sync + 'static`.
pub trait FunctionMutTrait: FnMut() + Send + Sync + 'static {}
impl<T> FunctionMutTrait for T where T: FnMut() + Send + Sync + 'static {}
/// Trait alias for asynchronous functions that can be executed in a recoverable context.
///
/// - Functions implementing this trait must return a `Future` and satisfy
/// `FnOnce() -> Future + Send + Sync + 'static`.
pub trait AsyncRecoverableFunction: Send + Sync + 'static {
type Output: Send;
type Future: Future<Output = Self::Output> + Send;
/// Executes the asynchronous function.
fn call(self) -> Self::Future;
}
impl<F, Fut, O> AsyncRecoverableFunction for F
where
F: FnOnce() -> Fut + Send + Sync + 'static,
Fut: Future<Output = O> + Send + 'static,
O: Send + 'static,
{
type Output = O;
type Future = Fut;
fn call(self) -> Self::Future {
self()
}
}
/// Trait alias for asynchronous error-handling functions used in a recoverable context.
///
/// - Functions implementing this trait must accept a `Arc<String>` as an error message,
/// return a `Future`, and satisfy `FnOnce(Arc<String>) -> Future + Send + Sync + 'static`.
pub trait AsyncErrorHandlerFunction: Send + Sync + 'static {
type Future: Future<Output = ()> + Send;
/// Handles an error asynchronously.
///
/// - `error`: The error message to handle.
fn call(self, error: Arc<String>) -> Self::Future;
}
impl<F, Fut> AsyncErrorHandlerFunction for F
where
F: FnOnce(Arc<String>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
type Future = Fut;
fn call(self, error: Arc<String>) -> Self::Future {
self(error)
}
}
/// Trait alias for functions that can be executed in a recoverable context.
///
/// - Functions implementing this trait must satisfy `FnOnce() + Send + Sync + 'static`.
pub trait RecoverableFunction: FnOnce() + Send + Sync + 'static {}
impl<T> RecoverableFunction for T where T: FnOnce() + Send + Sync + 'static {}
/// Trait alias for error-handling functions used in a recoverable context.
///
/// - Functions implementing this trait must accept a `&str` as an error message
/// and satisfy `FnOnce(&str) + Send + Sync + 'static`.
pub trait ErrorHandlerFunction: FnOnce(&str) + Send + Sync + 'static {}
impl<T> ErrorHandlerFunction for T where T: FnOnce(&str) + Send + Sync + 'static {}