-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
51 lines (41 loc) · 1.36 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use libc::{c_void};
use ::std::*;
extern "C" {
pub fn cpp_new_some_class() -> *mut c_void;
pub fn cpp_some_class_set_callback(instance: *mut c_void, callback_in_rust: *mut c_void);
pub fn cpp_some_class_set_rust_object(instance: *mut c_void, rust_object: *mut c_void);
}
unsafe extern "C" fn trampoline(this: *mut c_void, i: u32) {
let some_class = &*(this as *const SomeClass);
//let string_argument = CStr::from_ptr(string_argument).to_string_lossy();
some_class.do_something(i);
}
pub struct SomeClass(Box<Inner>);
struct Inner {
cpp_some_class_pointer: *mut c_void
}
impl SomeClass {
pub fn new_some_class()-> SomeClass {
let s = SomeClass(
Box::new(Inner{
cpp_some_class_pointer: unsafe {cpp_new_some_class()}
})
);
s.set_rust_object();
s.set_callback(trampoline as *mut c_void);
s
}
pub fn set_rust_object(&mut self) {
unsafe{cpp_some_class_set_rust_object(self.cpp_some_class_pointer, self.0 as *mut c_void)}
}
pub fn set_callback(&mut self, parent: *mut c_void) {
unsafe {cpp_some_class_set_callback(self.0 as *mut c_void, parent)}
}
pub fn do_something(&mut self, i: u32) {
println!("{}", i);
}
}
fn main() {
let s = SomeClass::new_some_class();
std::thread::sleep(std::time::Duration::from_secs(5));
}