use std::cell::Cell;
use std::panic::{self, AssertUnwindSafe};
use std::sync::Once;
thread_local! {
static CATCHING: Cell<bool> = const { Cell::new(false) };
}
pub fn catch_quietly<R>(f: impl FnOnce() -> R) -> std::thread::Result<R> {
let outer = CATCHING.replace(true);
let result = panic::catch_unwind(AssertUnwindSafe(f));
CATCHING.set(outer);
result
}
pub fn silence_caught_panics() {
static INSTALL: Once = Once::new();
INSTALL.call_once(|| {
let previous = panic::take_hook();
panic::set_hook(Box::new(move |info| {
if !CATCHING.get() {
previous(info);
}
}));
});
}