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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use leveldb_sys::*;
use libc::{size_t, c_void, c_char};
use std::slice;
use std::cmp::Ordering;
use database::key::Key;
use database::key::from_u8;
use std::marker::PhantomData;
pub trait Comparator {
type K: Key;
fn name(&self) -> *const c_char;
fn compare(&self, a: &Self::K, b: &Self::K) -> Ordering;
fn null() -> bool {
false
}
}
pub struct OrdComparator<K: Key + Ord> {
name: String,
marker: PhantomData<K>,
}
impl<K: Key + Ord> OrdComparator<K> {
pub fn new(name: &str) -> OrdComparator<K> {
OrdComparator {
marker: PhantomData,
name: name.to_string(),
}
}
}
#[derive(Copy,Clone)]
pub struct DefaultComparator;
unsafe trait InternalComparator : Comparator where Self: Sized {
extern "C" fn name(state: *mut c_void) -> *const c_char {
let x = unsafe { &*(state as *mut Self) };
x.name()
}
extern "C" fn compare(state: *mut c_void,
a: *const i8,
a_len: size_t,
b: *const i8,
b_len: size_t)
-> i32 {
unsafe {
let a_slice = slice::from_raw_parts::<u8>(a as *const u8, a_len as usize);
let b_slice = slice::from_raw_parts::<u8>(b as *const u8, b_len as usize);
let x = &*(state as *mut Self);
let a_key = from_u8::<<Self as Comparator>::K>(a_slice);
let b_key = from_u8::<<Self as Comparator>::K>(b_slice);
match x.compare(&a_key, &b_key) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
}
extern "C" fn destructor(state: *mut c_void) {
let _x: Box<Self> = unsafe { Box::from_raw(state as *mut Self) };
}
}
unsafe impl<C: Comparator> InternalComparator for C {}
#[allow(missing_docs)]
pub fn create_comparator<T: Comparator>(x: Box<T>) -> *mut leveldb_comparator_t {
unsafe {
leveldb_comparator_create(Box::into_raw(x) as *mut c_void,
<T as InternalComparator>::destructor,
<T as InternalComparator>::compare,
<T as InternalComparator>::name)
}
}
impl<K: Key + Ord> Comparator for OrdComparator<K> {
type K = K;
fn name(&self) -> *const c_char {
let slice: &str = self.name.as_ref();
slice.as_ptr() as *const c_char
}
fn compare(&self, a: &K, b: &K) -> Ordering {
a.cmp(b)
}
}
impl Comparator for DefaultComparator {
type K = i32;
fn name(&self) -> *const c_char {
"default_comparator".as_ptr() as *const c_char
}
fn compare(&self, _a: &i32, _b: &i32) -> Ordering {
Ordering::Equal
}
fn null() -> bool {
true
}
}