Skip to main content

catch

Function catch 

Source
pub fn catch<T, F>(body: F) -> Result<T, FfiError>
where F: FnOnce() -> Result<T, FfiError> + UnwindSafe,
Expand description

Run body, turning a panic into kind::PANIC instead of unwinding.

Use at every extern "C" entry point. A panic is a bug in the library rather than a classified failure of the caller’s input, so it is reported as such rather than being dressed up as a document problem.

The panic payload is not forwarded: a panic message is an implementation detail and often not 'static. What the caller needs is that it happened.

let ok: Result<i32, ffi::FfiError> = ffi::catch(|| Ok(7));
assert_eq!(ok.unwrap(), 7);

let boom: Result<i32, ffi::FfiError> = ffi::catch(|| panic!("bug"));
assert_eq!(boom.unwrap_err().0, kind::PANIC);