happylock 0.5.1

Free deadlock prevention
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};

/// Runs `try_fn`. If it unwinds, it will run `catch` and then continue
/// unwinding. This is used instead of `scopeguard` to ensure the `catch`
/// function doesn't run if the thread is already panicking. The unwind
/// must specifically be caused by the `try_fn`
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)
	})
}