use leveldb_sys::*;
use libc::{size_t, c_void, c_char};
use std::slice;
use std::cmp::Ordering;
pub trait Comparator {
fn name(&self) -> *const c_char;
fn compare(&self, a: &[u8], b: &[u8]) -> Ordering {
a.cmp(b)
}
fn null() -> bool {
false
}
}
#[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 c_char,
a_len: size_t,
b: *const c_char,
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);
match x.compare(&a_slice, &b_slice) {
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 Comparator for DefaultComparator {
fn name(&self) -> *const c_char {
"default_comparator".as_ptr() as *const c_char
}
fn null() -> bool {
true
}
}