blob: 070aa28fe6f1ed9cb30862971ead5810b54d356a [file] [log] [blame]
David Tolnay7db73692019-10-20 14:51:12 -04001use std::io::{self, Write};
2use std::panic::{self, UnwindSafe};
3use std::process;
4
5pub fn catch_unwind<F, R>(label: &'static str, foreign_call: F) -> R
6where
7 F: FnOnce() -> R + UnwindSafe,
8{
9 match panic::catch_unwind(foreign_call) {
10 Ok(ret) => ret,
11 Err(_) => abort(label),
12 }
13}
14
15#[cold]
16fn abort(label: &'static str) -> ! {
17 let mut stdout = io::stdout();
18 let _ = writeln!(stdout, "Error: panic in ffi function {}, aborting.", label);
19 process::abort();
20}