Function rmin::handle_panic

source ·
pub fn handle_panic<R, F: FnOnce() -> R + UnwindSafe>(f: F) -> R
Expand description

The most common function that may use close to the FFI boundary. This function could catch all possible panic and convert them into normal R error message.

§Usage:

handle_panic(||{
    let a=Vec::with_capacity(16);
    panic!("handle_panic will drop `a` even a panic is triggered.")
});
Examples found in repository?
examples/compare-rmin.rs (lines 5-9)
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
pub extern "C-unwind" fn add_protect(a:SEXP<f64>,b:SEXP<f64>) -> Owned<f64> {
    handle_panic(||{
        let mut c=Owned::new(1).protect();
        c[0]=a[0]+b[0];
        c.into()
    })
}
#[no_mangle]
pub extern "C-unwind" fn add_noprotect(a:SEXP<f64>,b:SEXP<f64>) -> Owned<f64> {
    handle_panic(||{
        let mut c=Owned::new(1);
        c[0]=a[0]+b[0];
        c
    })
}

/// raise panic.
#[no_mangle]
pub extern "C-unwind" fn panic() -> Owned<f64> {
    handle_panic(||{
        panic!("error occurs")
    })
}