Skip to content

Instantly share code, notes, and snippets.

@XeCycle
Created April 25, 2023 02:50
Show Gist options
  • Save XeCycle/beeb34e9e1539cc314fbec04a3fbeaa8 to your computer and use it in GitHub Desktop.
Save XeCycle/beeb34e9e1539cc314fbec04a3fbeaa8 to your computer and use it in GitHub Desktop.
#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);
}
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