recoverable_spawn/thread/sync/
fn.rs

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