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;
#[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()
}
}