fastcmp/lib.rs
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
//! # A fast byte comparison library
//!
//! The library is intended to provide a faster byte slice comparison than the standard library.
//! Also raw string literals `b"like this"` are compareable this way.
//!
//! ## Example usage
//!
//! ```rust
//! use fastcmp::Compare;
//!
//! let vec = vec![1, 2, 3, 4, 5];
//! assert!(vec.feq(&[1, 2, 3, 4, 5]));
//! ```
//#![feature(i128_type)]
include!(concat!(env!("OUT_DIR"), "/compare.rs"));
// The pointer compare macro with offset support
macro_rules! cmp (
($left:expr, $right: expr, $var:ident, $offset:expr) => {
unsafe {*($left.offset($offset) as *const $var) == *($right.offset($offset) as *const $var)}
}
);
/// Memory compare trait
pub trait Compare {
/// Compares an `&[u8]` to another one
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 {
// Fallback if the slices are too large
extern "C" {
fn memcmp(s1: *const i8, s2: *const i8, n: usize) -> i32;
}
// Get the comparison pointers
let a = to.as_ptr() as *const i8;
let b = self.as_ptr() as *const i8;
let len = to.len();
// Do the comparison
self.len() == len && slice_compare!(a, b, len)
}
}