| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame^] | 1 | use std::io::{self, Write}; |
| 2 | use std::panic::{self, UnwindSafe}; |
| 3 | use std::process; |
| 4 | |
| 5 | pub fn catch_unwind<F, R>(label: &'static str, foreign_call: F) -> R |
| 6 | where |
| 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] |
| 16 | fn 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 | } |