You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
22 lines
406 B
Rust
22 lines
406 B
Rust
5 years ago
|
use std::mem;
|
||
|
use std::process;
|
||
|
|
||
|
/// Calls a function and aborts if it panics.
|
||
|
///
|
||
|
/// This is useful in unsafe code where we can't recover from panics.
|
||
|
#[inline]
|
||
|
pub fn abort_on_panic<T>(f: impl FnOnce() -> T) -> T {
|
||
|
struct Bomb;
|
||
|
|
||
|
impl Drop for Bomb {
|
||
|
fn drop(&mut self) {
|
||
|
process::abort();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let bomb = Bomb;
|
||
|
let t = f();
|
||
|
mem::forget(bomb);
|
||
|
t
|
||
|
}
|