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
use std::{future::Future, sync::Arc};

/// 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 RecoverableFunction: 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> RecoverableFunction 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 `Fn(Arc<String>) -> Future + Send + Sync + 'static`.
pub trait ErrorHandlerFunction: 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> ErrorHandlerFunction for F
where
    F: Fn(Arc<String>) -> Fut + Send + Sync + 'static,
    Fut: Future<Output = ()> + Send + 'static,
{
    type Future = Fut;

    fn call(&self, error: Arc<String>) -> Self::Future {
        self(error)
    }
}