-
-
Save XeCycle/beeb34e9e1539cc314fbec04a3fbeaa8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| void test_rs_lib(void (*cb)(int, void*), void*); | |
| void my_cb(int a, void* ctx) | |
| { | |
| *((int*)ctx) = a; | |
| } | |
| int main() | |
| { | |
| int saved = 0; | |
| test_rs_lib(&my_cb, &saved); | |
| printf("%d\n", saved); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::ffi::c_void; | |
| struct CCb(unsafe extern "C" fn(i32, *mut c_void), *mut c_void); | |
| impl CCb { | |
| unsafe fn call(self, arg: i32) { | |
| self.0(arg, self.1) | |
| } | |
| } | |
| #[no_mangle] | |
| extern "C" fn test_rs_lib(cb: unsafe extern "C" fn(i32, *mut c_void), ctx: *mut c_void) { | |
| let ccb = CCb(cb, ctx); | |
| unsafe { | |
| ccb.call(42); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment