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
use std::cmp::Ordering;

use libc::memcmp;

/// In the most time of this project, the key or value will be represented by a so called Slice (actually
/// not a slice but a `Vec<u8>`).
///
/// It is not a good design (for both egineering and efficiency). But it's a compromise between programming
/// efficiency and runtime efficiency.
///
/// Good Part:
///
/// It avoids the battle with lifetime. With Future and async/await used in this project, complicated
/// lifetime should be avoided at most time. Or I will fall into the blackhole of lifetime contradiction.
///
/// Bad Part:
///
/// When facing the low level operation, or the memory is gotten from mmap, using vector may lead to
/// unexpected error. One of them is unexpected `free()`. While dropping a vector, it will automatically
/// free the memory owned by it. However, if I use `from_raw_parts` and make up a vector from memmap
/// memory, it will drop this memmaped memory and lead to a panic.
///
#[derive(Clone, Eq, Debug)]
pub struct Slice(pub Vec<u8>);

impl Default for Slice {
    fn default() -> Self {
        Slice(Vec::with_capacity(0))
    }
}

impl PartialOrd for Slice {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if self.0.len() < other.0.len() {
            Some(Ordering::Less)
        } else if self.0.len() > other.0.len() {
            Some(Ordering::Greater)
        } else {
            let res = unsafe {
                memcmp(
                    self.0.as_ptr() as *const core::ffi::c_void,
                    other.0.as_ptr() as *const core::ffi::c_void,
                    self.0.len(),
                )
            };
            if res == 0 {
                Some(Ordering::Equal)
            } else if res < 0 {
                Some(Ordering::Less)
            } else {
                Some(Ordering::Greater)
            }
        }
    }
}

impl PartialEq for Slice {
    fn eq(&self, other: &Self) -> bool {
        self.partial_cmp(other) == Some(Ordering::Equal)
    }
}

impl Ord for Slice {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap() // here it is safe.
    }
}