1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::any::Any;
use std::cell::RefCell;
use std::panic::{self, AssertUnwindSafe};
thread_local!(static LAST_ERROR: RefCell<Option<Box<dyn Any + Send>>> = {
RefCell::new(None)
});
pub fn catch<T, F: FnOnce() -> T>(f: F) -> Option<T> {
match LAST_ERROR.try_with(|slot| slot.borrow().is_some()) {
Ok(true) => return None,
Ok(false) => {}
Err(_) => {}
}
match panic::catch_unwind(AssertUnwindSafe(f)) {
Ok(ret) => Some(ret),
Err(e) => {
LAST_ERROR.with(|slot| *slot.borrow_mut() = Some(e));
None
}
}
}
pub fn propagate() {
if let Ok(Some(t)) = LAST_ERROR.try_with(|slot| slot.borrow_mut().take()) {
panic::resume_unwind(t)
}
}