summaryrefslogtreecommitdiff
path: root/src/handle_unwind.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/handle_unwind.rs')
-rw-r--r--src/handle_unwind.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/handle_unwind.rs b/src/handle_unwind.rs
new file mode 100644
index 0000000..d515449
--- /dev/null
+++ b/src/handle_unwind.rs
@@ -0,0 +1,9 @@
+use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};
+
+pub fn handle_unwind<R, F: FnOnce() -> R, G: FnOnce()>(try_fn: F, catch: G) -> R {
+ let try_fn = AssertUnwindSafe(try_fn);
+ catch_unwind(try_fn).unwrap_or_else(|e| {
+ catch();
+ resume_unwind(e)
+ })
+}