1use std::panic;
2use std::process;
3
4use ffi::*;
5use libc::{c_int, c_void};
6
7pub struct Interrupt {
8 pub interrupt: AVIOInterruptCB,
9}
10
11extern "C" fn callback<F>(opaque: *mut c_void) -> c_int
12where
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
23pub fn new<F>(opaque: Box<F>) -> Interrupt
24where
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