summaryrefslogtreecommitdiff
path: root/src/handle_unwind.rs
blob: e00bbbd0902084bef25fa5c0edfe4ac9825231d1 (plain)
#[cfg(feature = "std")]
use std::panic::{catch_unwind, resume_unwind, AssertUnwindSafe};

cfg_select! {
	feature = "std" => {
		/// 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`
		///
		/// This has no effect in `no_std` environments
		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)
			})
		}
	}
	_ => {
		/// 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`
		///
		/// This has no effect in `no_std` environments
		pub fn handle_unwind<R, F: FnOnce() -> R, G: FnOnce()>(try_fn: F, _catch: G) -> R {
			try_fn()
		}
	}
}