1--- 2source: macro/src/lib.rs 3assertion_line: 858 4expression: "rustfmt_code(&gen_bridge(parse_quote! {\n mod ffi\n {\n pub struct Wrapper { cant_be_empty: bool, } pub struct\n TestingStruct { x: i32, y: i32, } impl Wrapper\n {\n pub fn\n 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\n fn test_cb_with_struct(f: impl Fn(TestingStruct) -> i32) ->\n i32 { let arg = TestingStruct { x: 1, y: 5, }; f(arg) } pub\n fn\n test_multiple_cb_args(f: impl Fn() -> i32, g: impl Fn(i32)\n -> i32) -> i32 { f() + g(5) }\n }\n }\n }).to_token_stream().to_string())" 5--- 6mod ffi { 7 #[repr(C)] 8 pub struct Wrapper { 9 cant_be_empty: bool, 10 } 11 #[repr(C)] 12 pub struct TestingStruct { 13 x: i32, 14 y: i32, 15 } 16 impl Wrapper { 17 pub fn test_multi_arg_callback(f: impl Fn(i32) -> i32, x: i32) -> i32 { 18 f(10 + x) 19 } 20 pub fn test_multiarg_void_callback(f: impl Fn(i32, &str)) { 21 f(-10, "hello it's a string\0"); 22 } 23 pub fn test_mod_array(g: impl Fn(&[u8])) { 24 let bytes: Vec<u8> = vec![0x11, 0x22]; 25 g(bytes.as_slice().into()); 26 } 27 pub fn test_no_args(h: impl Fn()) -> i32 { 28 h(); 29 -5 30 } 31 pub fn test_cb_with_struct(f: impl Fn(TestingStruct) -> i32) -> i32 { 32 let arg = TestingStruct { x: 1, y: 5 }; 33 f(arg) 34 } 35 pub fn test_multiple_cb_args(f: impl Fn() -> i32, g: impl Fn(i32) -> i32) -> i32 { 36 f() + g(5) 37 } 38 } 39 use core::ffi::c_void; 40 use diplomat_runtime::*; 41 #[no_mangle] 42 extern "C" fn Wrapper_test_multi_arg_callback(f: DiplomatCallback<i32>, x: i32) -> i32 { 43 let f = move |arg0: i32| unsafe { 44 std::mem::transmute::< 45 unsafe extern "C" fn(*const c_void, ...) -> i32, 46 unsafe extern "C" fn(*const c_void, i32) -> i32, 47 >(f.run_callback)(f.data, arg0) 48 }; 49 Wrapper::test_multi_arg_callback(f, x) 50 } 51 #[no_mangle] 52 extern "C" fn Wrapper_test_multiarg_void_callback(f: DiplomatCallback<()>) { 53 let f = move |arg0: i32, arg1: &str| unsafe { 54 let arg1: diplomat_runtime::DiplomatUtf8StrSlice = arg1.into(); 55 std::mem::transmute::< 56 unsafe extern "C" fn(*const c_void, ...) -> (), 57 unsafe extern "C" fn( 58 *const c_void, 59 i32, 60 diplomat_runtime::DiplomatUtf8StrSlice, 61 ) -> (), 62 >(f.run_callback)(f.data, arg0, arg1) 63 }; 64 Wrapper::test_multiarg_void_callback(f) 65 } 66 #[no_mangle] 67 extern "C" fn Wrapper_test_mod_array(g: DiplomatCallback<()>) { 68 let g = move |arg0: &[u8]| unsafe { 69 let arg0: diplomat_runtime::DiplomatSlice<u8> = arg0.into(); 70 std::mem::transmute::< 71 unsafe extern "C" fn(*const c_void, ...) -> (), 72 unsafe extern "C" fn(*const c_void, diplomat_runtime::DiplomatSlice<u8>) -> (), 73 >(g.run_callback)(g.data, arg0) 74 }; 75 Wrapper::test_mod_array(g) 76 } 77 #[no_mangle] 78 extern "C" fn Wrapper_test_no_args(h: DiplomatCallback<()>) -> i32 { 79 let h = move || unsafe { 80 std::mem::transmute::< 81 unsafe extern "C" fn(*const c_void, ...) -> (), 82 unsafe extern "C" fn(*const c_void) -> (), 83 >(h.run_callback)(h.data) 84 }; 85 Wrapper::test_no_args(h) 86 } 87 #[no_mangle] 88 extern "C" fn Wrapper_test_cb_with_struct(f: DiplomatCallback<i32>) -> i32 { 89 let f = move |arg0: TestingStruct| unsafe { 90 std::mem::transmute::< 91 unsafe extern "C" fn(*const c_void, ...) -> i32, 92 unsafe extern "C" fn(*const c_void, TestingStruct) -> i32, 93 >(f.run_callback)(f.data, arg0) 94 }; 95 Wrapper::test_cb_with_struct(f) 96 } 97 #[no_mangle] 98 extern "C" fn Wrapper_test_multiple_cb_args( 99 f: DiplomatCallback<i32>, 100 g: DiplomatCallback<i32>, 101 ) -> i32 { 102 let f = move || unsafe { 103 std::mem::transmute::< 104 unsafe extern "C" fn(*const c_void, ...) -> i32, 105 unsafe extern "C" fn(*const c_void) -> i32, 106 >(f.run_callback)(f.data) 107 }; 108 let g = move |arg0: i32| unsafe { 109 std::mem::transmute::< 110 unsafe extern "C" fn(*const c_void, ...) -> i32, 111 unsafe extern "C" fn(*const c_void, i32) -> i32, 112 >(g.run_callback)(g.data, arg0) 113 }; 114 Wrapper::test_multiple_cb_args(f, g) 115 } 116} 117