Created
October 28, 2012 22:29
-
-
Save eevee/3970206 to your computer and use it in GitHub Desktop.
passing a rust function as a C callback
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
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))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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