ffmpeg_the_third/util/
interrupt.rs

1use std::panic;
2use std::process;
3
4use crate::ffi::*;
5use libc::{c_int, c_void};
6
7pub struct Interrupt {
8    pub interrupt: AVIOInterruptCB,
9}
10
11#[allow(clippy::needless_borrow)]
12extern "C" fn callback<F>(opaque: *mut c_void) -> c_int
13where
14    F: FnMut() -> bool,
15{
16    match panic::catch_unwind(|| (unsafe { &mut *(opaque as *mut F) })()) {
17        Ok(ret) => ret as c_int,
18        Err(_) => process::abort(),
19    }
20}
21
22pub fn new<F>(opaque: Box<F>) -> Interrupt
23where
24    F: FnMut() -> bool,
25{
26    let interrupt_cb = AVIOInterruptCB {
27        callback: Some(callback::<F>),
28        opaque: Box::into_raw(opaque) as *mut c_void,
29    };
30    Interrupt {
31        interrupt: interrupt_cb,
32    }
33}