-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.rs
38 lines (34 loc) · 883 Bytes
/
phantom.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
use std::marker::PhantomData;
#[derive(Debug, PartialEq)]
struct Hash<T> {
h: u128,
p: PhantomData<T>,
}
unsafe fn hasher(start: *const u8, nbytes: usize) -> u128 {
let mut result: u128 = 0;
for i in 0..nbytes {
let b = *start.offset(i as isize);
result = result.wrapping_add(b as u128);
}
result
}
impl<T> Hash<T> {
fn hash(val: &T) -> Hash<T> {
let h = unsafe { hasher(
std::ptr::addr_of!(*val) as *const u8,
std::mem::size_of::<T>(),
)};
Hash { h, p: PhantomData }
}
}
fn main() {
let h1 = Hash::hash(&(0i32, 1i32));
println!("{:?}", h1);
let h2 = Hash::hash(&1u64);
println!("{:?}", h2);
let h3 = Hash::hash(&(0i32, 1i32));
println!("{:?}", h3);
// Can't even compare these, since they are of
// different type.
// println!("{}", h1 == h2);
}