libcqdb/
macros.rs

1// Taken from https://github.com/hyperium/hyper/blob/master/src/ffi/macros.rs
2macro_rules! ffi_fn {
3    ($(#[$doc:meta])* fn $name:ident($($arg:ident: $arg_ty:ty),*) -> $ret:ty $body:block) => {
4        $(#[$doc])*
5        #[no_mangle]
6        pub extern fn $name($($arg: $arg_ty),*) -> $ret {
7            use std::panic::{self, AssertUnwindSafe};
8
9            match panic::catch_unwind(AssertUnwindSafe(move || $body)) {
10                Ok(v) => v,
11                Err(_) => {
12                    // TODO: We shouldn't abort, but rather figure out how to
13                    // convert into the return type that the function errored.
14                    eprintln!("panic unwind caught, aborting");
15                    std::process::abort();
16                }
17            }
18        }
19    };
20
21    ($(#[$doc:meta])* fn $name:ident($($arg:ident: $arg_ty:ty),*) $body:block) => {
22        ffi_fn!($(#[$doc])* fn $name($($arg: $arg_ty),*) -> () $body);
23    };
24}