use std::any::Any;
use std::cell::RefCell;
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
thread_local!(static PANIC_PAYLOAD: RefCell<Option<Box<dyn Any + Send>>> = RefCell::new(None));
pub fn maybe_resume_unwind() {
if let Some(error) = PANIC_PAYLOAD.with(|result| result.borrow_mut().take()) {
resume_unwind(error);
}
}
#[inline(never)]
pub fn wrap_panic(function: &mut dyn FnMut()) {
match catch_unwind(AssertUnwindSafe(function)) {
Ok(()) => {}
Err(payload) => {
PANIC_PAYLOAD.with(|opt_payload| {
let mut opt_payload = opt_payload.borrow_mut();
assert!(opt_payload.is_none());
*opt_payload = Some(payload);
});
}
}
}