use mem_isolate::{MemIsolateError, execute_in_isolated_process};
const MY_FUNCTION_IS_IDEMPOTENT: bool = true;
const PREFER_RETRIES_WITH_MEM_ISOLATE: bool = true;
fn my_function() -> Result<String, String> {
use rand::Rng;
let mut rng = rand::rng();
if rng.random::<f32>() > 0.5 {
return Err("Your supplied function non-deterministically failed".to_string());
}
Ok("Your function executed without a hitch!".to_string())
}
fn main() -> Result<(), MemIsolateError> {
let mem_isolate_result = execute_in_isolated_process(my_function);
let my_function_result = match mem_isolate_result {
Ok(my_funct_result) => my_funct_result,
Err(MemIsolateError::CallableExecuted(ref _err)) => {
mem_isolate_result?
}
Err(MemIsolateError::CallableDidNotExecute(ref _err)) => {
if PREFER_RETRIES_WITH_MEM_ISOLATE {
execute_in_isolated_process(my_function)?
} else {
my_function()
}
}
Err(MemIsolateError::CallableStatusUnknown(ref _err)) => {
if MY_FUNCTION_IS_IDEMPOTENT {
my_function()
} else {
panic!("Callable is not idempotent, and we don't know the status of the callable");
}
}
};
match my_function_result {
Ok(result) => {
println!("{result}");
}
Err(e) => {
eprintln!("{e}");
}
}
Ok(())
}
#[cfg(test)]
mod example_error_handling_basic {
use super::*;
#[test]
fn execute_main() {
main().unwrap();
}
}