Skip to content

Instantly share code, notes, and snippets.

@eevee
Created October 28, 2012 22:29
Show Gist options
  • Save eevee/3970206 to your computer and use it in GitHub Desktop.
Save eevee/3970206 to your computer and use it in GitHub Desktop.
passing a rust function as a C callback
extern fn status_foreach_callback(path: *c_char, stat: c_uint, payload: *c_void) -> c_int unsafe {
let path_str = str::raw::from_c_str(path);
let f: *fn(&str, uint) = cast::reinterpret_cast(&payload);
(*f)(path_str, stat as uint);
return 0;
}
// ...
impl Repository {
fn for_status(f: fn(&str, uint)) unsafe {
c::git_status_foreach(self.c_repository, status_foreach_callback, cast::reinterpret_cast(&ptr::addr_of(&f)));
}
}
@amuxtux
Copy link

amuxtux commented Nov 15, 2012

how are you fetching and executing the function pointer status_foreach_callback in C ?
when I try it gives me Illegal Instruction error.
If I try to type cast the pointer in rust, it gives me segmentation fault.

looking forward for any reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment