#![no_std]
extern crate alloc;
use alloc::vec::Vec;
use core::hash::Hasher;
use libc::{c_uint, c_void, free, malloc, ptrdiff_t};
extern "C" {
fn chibihash64__load64le(p: *const c_uint) -> u64;
fn chibihash64(keyIn: *const c_void, len: ptrdiff_t, seed: u64) -> u64;
}
#[derive(Debug, Clone)]
pub struct Chibihash {
seed: u64,
buffer: Vec<u8>,
}
impl Hasher for Chibihash {
fn finish(&self) -> u64 {
self.hash(&self.buffer).0
}
fn write(&mut self, bytes: &[u8]) {
self.buffer.extend_from_slice(bytes);
}
}
impl Chibihash {
pub fn new(seed: u32) -> Self {
Self::with_capcity(seed, 1024)
}
pub fn with_capcity(seed: u32, capacity: usize) -> Self {
unsafe {
let seed_ptr = malloc(size_of::<c_uint>()) as *mut c_uint;
*seed_ptr = seed;
let seed = chibihash64__load64le(seed_ptr as *const c_uint);
free(seed_ptr as *mut c_void);
Chibihash {
seed,
buffer: Vec::with_capacity(capacity),
}
}
}
pub fn hash(&self, key: &[u8]) -> Hash {
unsafe {
let res = chibihash64(
key.as_ptr() as *const c_void,
core::mem::size_of_val(key) as ptrdiff_t,
self.seed,
);
Hash(res)
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct Hash(pub u64);
impl Hash {
pub fn to_be_bytes(&self) -> [u8; 8] {
self.0.to_be_bytes()
}
pub fn to_le_bytes(&self) -> [u8; 8] {
self.0.to_le_bytes()
}
pub fn to_ne_bytes(&self) -> [u8; 8] {
self.0.to_ne_bytes()
}
pub fn as_u64(&self) -> u64 {
self.0
}
}
#[cfg(test)]
pub mod test {
use super::*;
#[test]
fn basic() {
let chibi = Chibihash::new(0);
let key_in = [0u8; 128];
let res = chibi.hash(&key_in);
assert_eq!(res.0, 1977729916931055241);
}
}