memory_cache_rust/bloom/
rutil.rs

1use std::hash::{Hash, Hasher};
2
3pub type NanoTime = Box<dyn Fn(i64) -> i64>;
4pub type CPUTicks = Box<dyn Fn(i64) -> i64>;
5pub type Memhash = Box<dyn Fn(*const str, *const usize, *const usize) -> u64>;
6
7unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
8    ::std::slice::from_raw_parts(
9        (p as *const T) as *const u8,
10        ::std::mem::size_of::<T>(),
11    )
12}
13
14
15#[repr(C)]
16pub struct StringStruct([u8; 32]);
17
18impl Hash for StringStruct {
19    fn hash<H: Hasher>(&self, state: &mut H) {
20        self.0.hash(state)
21    }
22}
23
24/// mem_hash is the hash function used by go map, it utilizes available hardware instructions(behaves
25/// as aeshash if aes instruction is available).
26/// NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
27pub fn mem_hash(data: &[u8]) -> u64 {
28    // let (head, body, _tail) = unsafe { data.align_to::<StringStruct>() };
29    // assert!(head.is_empty(), "Data was not aligned");
30    // let my_struct = &body[0];
31
32    let hash = seahash::hash(data);
33    // let my_struct =   unsafe { data.as_mut_ptr() as *mut StringStruct };
34    // let mut s = DefaultHasher::new();
35    // my_struct.hash(&mut s);
36    // let hash = s.finish();
37    hash
38}
39
40/// mem_hash is the hash function used by go map, it utilizes available hardware instructions(behaves
41/// as aeshash if aes instruction is available).
42/// NOTE: The hash seed changes for every process. So, this cannot be used as a persistent hash.
43pub fn mem_hash_byte(data: &[u8]) -> u64 {
44    // let (head, body, _tail) = unsafe { data.align_to::<StringStruct>() };
45    // assert!(head.is_empty(), "Data was not aligned");
46    // let my_struct = &body[0];
47
48    let hash = seahash::hash(data);
49    // let my_struct =   unsafe { data.as_mut_ptr() as *mut StringStruct };
50    // let mut s = DefaultHasher::new();
51    // my_struct.hash(&mut s);
52    // let hash = s.finish();
53    hash
54}