-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
39 lines (33 loc) · 860 Bytes
/
lib.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
#![crate_type = "staticlib"]
#![allow(dead_code)]
use std::thread;
use std::time::Duration;
use std::ffi::c_void;
#[repr(C)]
pub struct CompletedCallback {
userdata: *mut c_void,
callback: extern "C" fn(*mut c_void, bool),
}
unsafe impl Send for CompletedCallback {}
impl CompletedCallback {
fn succeeded(self) {
(self.callback)(self.userdata, true);
std::mem::forget(self)
}
fn failed(self) {
(self.callback)(self.userdata, false);
std::mem::forget(self)
}
}
impl Drop for CompletedCallback {
fn drop(&mut self) {
panic!("CompletedCallback must have explicit succeeded or failed call")
}
}
#[no_mangle]
pub extern "C" fn async_operation(callback: CompletedCallback) {
thread::spawn(move || {
thread::sleep(Duration::from_secs(3));
callback.succeeded()
});
}