Macro abi_stable::extern_fn_panic_handling

source ·
macro_rules! extern_fn_panic_handling {
    (no_early_return; $($fn_contents:tt)* ) => { ... };
    ( $($fn_contents:tt)* ) => { ... };
}
Expand description

Use this to make sure that you handle panics inside extern fn correctly.

This macro causes an abort if a panic reaches this point.

It does not prevent functions inside from using ::std::panic::catch_unwind to catch the panic.

§Early returns

This macro by default wraps the passed code in a closure so that any early returns that happen inside don’t interfere with the macro generated code.

If you don’t have an early return (a return/continue/break/?/etc.) in the code passed to this macro you can use extern_fn_panic_handling!{no_early_return; <code here> }, which might be cheaper(this has not been tested yet).

§Example

use std::fmt;

use abi_stable::{
    extern_fn_panic_handling,
    std_types::RString,
};


pub extern "C" fn print_debug<T>(this: &T, buf: &mut RString)
where
    T: fmt::Debug,
{
    extern_fn_panic_handling! {
        use std::fmt::Write;

        println!("{:?}", this);
    }
}

§Example, no_early_return

use std::fmt;

use abi_stable::{
    extern_fn_panic_handling,
    std_types::RString,
};


pub extern "C" fn print_debug<T>(this: &T, buf: &mut RString)
where
    T: fmt::Debug,
{
    extern_fn_panic_handling!{no_early_return;
        use std::fmt::Write;

        println!("{:?}", this);
    }
}

§Returing in no_early_return

Attempting to do any kind of returning from inside of extern_fn_panic_handling!{no_early_return} will cause an abort:

use abi_stable::extern_fn_panic_handling;

pub extern "C" fn function() {
    extern_fn_panic_handling!{no_early_return;
        return;
    }
}

function();