Skip to content

Instantly share code, notes, and snippets.

@emoon
Created October 12, 2015 12:50
Show Gist options
  • Save emoon/7268ad55d770f3f18361 to your computer and use it in GitHub Desktop.
Save emoon/7268ad55d770f3f18361 to your computer and use it in GitHub Desktop.
extern crate libc;
#[repr(C)]
struct ApiFromC {
some_data: *mut libc::c_void,
some_function: extern fn(data: *mut libc::c_void),
// ...
}
struct RustAPI {
api_from_c: *mut ApiFromC,
}
impl RustAPI {
fn call_c_api(&self) {
unsafe {
(*self.api_from_c).some_function((*self.api_from_c).some_data)
}
}
}
#[no_mangle]
pub extern fn call_from_c(api: *mut ApiFromC) {
let test = RustAPI { api_from_c : api };
test.call_c_api();
}
// error
Compiling api_test v0.1.0 (file:///C:/Users/dcollin/Documents/api_test)
src\lib.rs:17:23: 17:66 error: no method named `some_function` found for type `ApiFromC` in the current scope
src\lib.rs:17 (*self.api_from_c).some_function((*self.api_from_c).some_data)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src\lib.rs:17:23: 17:66 note: did you mean to write `(*self.api_from_c).some_function`?
src\lib.rs:17 (*self.api_from_c).some_function((*self.api_from_c).some_data)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `api_test`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment