diplomat 0.14.0

The diplomat FFI generation macro
Documentation
---
source: macro/src/lib.rs
expression: "pretty_print_code(gen_bridge(parse_quote!\n{\n    mod ffi\n    {\n        pub struct Wrapper { cant_be_empty: bool, } pub struct TestingStruct\n        { x: i32, y: i32, } impl Wrapper\n        {\n            pub fn test_multi_arg_callback(f: impl Fn(i32) -> i32, x: i32) ->\n            i32 { f(10 + x) } pub fn\n            test_multiarg_void_callback(f: impl Fn(i32, &str))\n            { f(-10, \"hello it's a string\\0\"); } pub fn\n            test_mod_array(g: impl Fn(&[u8]))\n            {\n                let bytes: Vec<u8> = vec![0x11, 0x22];\n                g(bytes.as_slice().into());\n            } pub fn test_no_args(h: impl Fn()) -> i32 { h(); -5 } pub fn\n            test_cb_with_struct(f: impl Fn(TestingStruct) -> i32) -> i32\n            { let arg = TestingStruct { x: 1, y: 5, }; f(arg) } pub fn\n            test_multiple_cb_args(f: impl Fn() -> i32, g: impl Fn(i32) -> i32)\n            -> i32 { f() + g(5) }\n        }\n    }\n}).to_token_stream())"
---
mod ffi {
    #[repr(C)]
    pub struct Wrapper {
        cant_be_empty: bool,
    }
    #[repr(C)]
    pub struct TestingStruct {
        x: i32,
        y: i32,
    }
    impl Wrapper {
        pub fn test_multi_arg_callback(f: impl Fn(i32) -> i32, x: i32) -> i32 {
            f(10 + x)
        }
        pub fn test_multiarg_void_callback(f: impl Fn(i32, &str)) {
            f(-10, "hello it's a string\0");
        }
        pub fn test_mod_array(g: impl Fn(&[u8])) {
            let bytes: Vec<u8> = vec![0x11, 0x22];
            g(bytes.as_slice().into());
        }
        pub fn test_no_args(h: impl Fn()) -> i32 {
            h();
            -5
        }
        pub fn test_cb_with_struct(f: impl Fn(TestingStruct) -> i32) -> i32 {
            let arg = TestingStruct { x: 1, y: 5 };
            f(arg)
        }
        pub fn test_multiple_cb_args(
            f: impl Fn() -> i32,
            g: impl Fn(i32) -> i32,
        ) -> i32 {
            f() + g(5)
        }
    }
    use diplomat_runtime::*;
    use core::ffi::c_void;
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_multi_arg_callback(
        f: DiplomatCallback<i32>,
        x: i32,
    ) -> i32 {
        let f = move |arg0: i32| unsafe {
            let _ = &f;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> i32,
                unsafe extern "C" fn(*const c_void, i32) -> i32,
            >(f.run_callback)(f.data, arg0)
        };
        Wrapper::test_multi_arg_callback(f, x)
    }
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_multiarg_void_callback(f: DiplomatCallback<()>) {
        let f = move |arg0: i32, arg1: &str| unsafe {
            let arg1: diplomat_runtime::DiplomatUtf8StrSlice = arg1.into();
            let _ = &f;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> (),
                unsafe extern "C" fn(
                    *const c_void,
                    i32,
                    diplomat_runtime::DiplomatUtf8StrSlice,
                ) -> (),
            >(f.run_callback)(f.data, arg0, arg1)
                .into()
        };
        Wrapper::test_multiarg_void_callback(f)
    }
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_mod_array(g: DiplomatCallback<()>) {
        let g = move |arg0: &[u8]| unsafe {
            let arg0: diplomat_runtime::DiplomatSlice<u8> = arg0.into();
            let _ = &g;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> (),
                unsafe extern "C" fn(
                    *const c_void,
                    diplomat_runtime::DiplomatSlice<u8>,
                ) -> (),
            >(g.run_callback)(g.data, arg0)
                .into()
        };
        Wrapper::test_mod_array(g)
    }
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_no_args(h: DiplomatCallback<()>) -> i32 {
        let h = move || unsafe {
            let _ = &h;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> (),
                unsafe extern "C" fn(*const c_void) -> (),
            >(h.run_callback)(h.data)
                .into()
        };
        Wrapper::test_no_args(h)
    }
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_cb_with_struct(f: DiplomatCallback<i32>) -> i32 {
        let f = move |arg0: TestingStruct| unsafe {
            let _ = &f;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> i32,
                unsafe extern "C" fn(*const c_void, TestingStruct) -> i32,
            >(f.run_callback)(f.data, arg0)
        };
        Wrapper::test_cb_with_struct(f)
    }
    #[no_mangle]
    #[allow(deprecated)]
    extern "C" fn Wrapper_test_multiple_cb_args(
        f: DiplomatCallback<i32>,
        g: DiplomatCallback<i32>,
    ) -> i32 {
        let f = move || unsafe {
            let _ = &f;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> i32,
                unsafe extern "C" fn(*const c_void) -> i32,
            >(f.run_callback)(f.data)
        };
        let g = move |arg0: i32| unsafe {
            let _ = &g;
            std::mem::transmute::<
                unsafe extern "C" fn(*mut c_void, ...) -> i32,
                unsafe extern "C" fn(*const c_void, i32) -> i32,
            >(g.run_callback)(g.data, arg0)
        };
        Wrapper::test_multiple_cb_args(f, g)
    }
}