recoverable_spawn/thread/async/
fn.rs

1use crate::*;
2use tokio::task::JoinError;
3
4/// Executes a recoverable function within a panic-safe context.
5///
6/// # Arguments
7///
8/// - `F` - Function implementing AsyncRecoverableFunction.
9///
10/// # Returns
11///
12/// - `AsyncSpawnResult` - The spawn operation result.
13pub async fn async_run_function<F: AsyncRecoverableFunction>(func: F) -> AsyncSpawnResult {
14    set_hook(Box::new(move |_| {}));
15    let func = async move {
16        func.call().await;
17    };
18    tokio::spawn(func).await
19}
20
21/// Executes an error-handling function within a panic-safe context.
22///
23/// # Arguments
24///
25/// - `E` - Function implementing AsyncErrorHandlerFunction.
26/// - `Arc<String>` - The error message.
27///
28/// # Returns
29///
30/// - `AsyncSpawnResult` - The spawn operation result.
31pub async fn async_run_error_handle_function<E: AsyncErrorHandlerFunction>(
32    func: E,
33    error: Arc<String>,
34) -> AsyncSpawnResult {
35    set_hook(Box::new(move |_| {}));
36    let func = async move {
37        func.call(error.clone()).await;
38    };
39    tokio::spawn(func).await
40}
41
42/// Converts a panic-captured error value into a string.
43///
44/// # Arguments
45///
46/// - `&JoinError` - The captured error value.
47///
48/// # Returns
49///
50/// - `String` - The error string representation.
51pub fn tokio_error_to_string(err: &JoinError) -> String {
52    err.to_string()
53}
54
55/// Spawns a recoverable function.
56///
57/// # Arguments
58///
59/// - `F` - Function implementing AsyncRecoverableFunction.
60///
61/// # Returns
62///
63/// - `AsyncSpawnResult` - The spawn operation result.
64pub async fn async_recoverable_spawn<F>(function: F) -> AsyncSpawnResult
65where
66    F: AsyncRecoverableFunction,
67{
68    async_run_function(function).await
69}
70
71/// Spawns a recoverable function with error handling.
72///
73/// # Arguments
74///
75/// - `F` - Function implementing AsyncRecoverableFunction.
76/// - `E` - Function implementing AsyncErrorHandlerFunction.
77///
78/// # Returns
79///
80/// - `AsyncSpawnResult` - The spawn operation result.
81pub async fn async_recoverable_spawn_catch<F, E>(
82    function: F,
83    error_handle_function: E,
84) -> AsyncSpawnResult
85where
86    F: AsyncRecoverableFunction,
87    E: AsyncErrorHandlerFunction,
88{
89    let run_result: AsyncSpawnResult = async_run_function(function).await;
90    if let Err(err) = run_result.as_ref() {
91        let err_string: String = tokio_error_to_string(err);
92        let _: AsyncSpawnResult =
93            async_run_error_handle_function(error_handle_function, Arc::new(err_string)).await;
94    }
95    run_result
96}
97
98/// Spawns a recoverable function with error handling and finalization.
99///
100/// # Arguments
101///
102/// - `F` - Function implementing AsyncRecoverableFunction.
103/// - `E` - Function implementing AsyncErrorHandlerFunction.
104/// - `L` - Function implementing AsyncRecoverableFunction.
105///
106/// # Returns
107///
108/// - `AsyncSpawnResult` - The spawn operation result.
109pub async fn async_recoverable_spawn_catch_finally<F, E, L>(
110    function: F,
111    error_handle_function: E,
112    finally: L,
113) -> AsyncSpawnResult
114where
115    F: AsyncRecoverableFunction,
116    E: AsyncErrorHandlerFunction,
117    L: AsyncRecoverableFunction,
118{
119    let run_result: AsyncSpawnResult = async_run_function(function).await;
120    if let Err(err) = run_result.as_ref() {
121        let err_string: String = tokio_error_to_string(err);
122        let _: AsyncSpawnResult =
123            async_run_error_handle_function(error_handle_function, Arc::new(err_string)).await;
124    }
125    let _: AsyncSpawnResult = async_run_function(finally).await;
126    run_result
127}