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
52
53
54
55
extern crate libc;
use self::libc::{c_char, c_int, c_void, size_t};
use std::ffi::CString;
use std::mem;
use std::slice;
pub struct ComparatorCallback {
pub name: CString,
pub f: fn(&[u8], &[u8]) -> i32,
}
pub extern "C" fn destructor_callback(raw_cb: *mut c_void) {
let _: Box<ComparatorCallback> = unsafe { mem::transmute(raw_cb) };
}
pub extern "C" fn name_callback(raw_cb: *mut c_void) -> *const c_char {
unsafe {
let cb: &mut ComparatorCallback =
&mut *(raw_cb as *mut ComparatorCallback);
let ptr = cb.name.as_ptr();
ptr as *const c_char
}
}
pub extern "C" fn compare_callback(raw_cb: *mut c_void,
a_raw: *const c_char,
a_len: size_t,
b_raw: *const c_char,
b_len: size_t)
-> c_int {
unsafe {
let cb: &mut ComparatorCallback =
&mut *(raw_cb as *mut ComparatorCallback);
let a: &[u8] = slice::from_raw_parts(a_raw as *const u8,
a_len as usize);
let b: &[u8] = slice::from_raw_parts(b_raw as *const u8,
b_len as usize);
(cb.f)(a, b)
}
}