| 1 | use std::panic; |
| 2 | use std::process; |
| 3 | |
| 4 | use ffi::*; |
| 5 | use libc::{c_int, c_void}; |
| 6 | |
| 7 | pub struct Interrupt { |
| 8 | pub interrupt: AVIOInterruptCB, |
| 9 | } |
| 10 | |
| 11 | extern "C" fn callback<F>(opaque: *mut c_void) -> c_int |
| 12 | where |
| 13 | F: FnMut() -> bool, |
| 14 | { |
| 15 | // Clippy suggests to remove &mut, but it doesn't compile then (move occurs because value has type `F`, which does not implement the `Copy` trait) |
| 16 | #[allow (clippy::needless_borrow)] |
| 17 | match panic::catch_unwind(|| (unsafe { &mut *(opaque as *mut F) })()) { |
| 18 | Ok(ret: bool) => ret as c_int, |
| 19 | Err(_) => process::abort(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn new<F>(opaque: Box<F>) -> Interrupt |
| 24 | where |
| 25 | F: FnMut() -> bool, |
| 26 | { |
| 27 | let interrupt_cb = AVIOInterruptCB { |
| 28 | callback: Some(callback::<F>), |
| 29 | opaque: Box::into_raw(opaque) as *mut c_void, |
| 30 | }; |
| 31 | Interrupt { |
| 32 | interrupt: interrupt_cb, |
| 33 | } |
| 34 | } |
| 35 | |