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
include!(concat!(env!("OUT_DIR"), "/compare.rs"));
macro_rules! cmp (
($left:expr, $right: expr, $var:ident, $offset:expr) => {
unsafe {*($left.offset($offset) as *const $var) == *($right.offset($offset) as *const $var)}
}
);
pub trait Compare {
fn feq(self: &Self, to: &Self) -> bool;
}
impl Compare for [u8] {
#[cfg_attr(feature = "cargo-clippy", allow(inline_always))]
#[inline(always)]
fn feq(&self, to: &[u8]) -> bool {
extern "C" {
fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32;
}
let a = to.as_ptr() as *const i8;
let b = self.as_ptr() as *const i8;
let len = to.len();
self.len() == len && slice_compare!(a, b, len)
}
}