1use core::hash::{Hash, Hasher};
5
6use serde::{Deserialize, Serialize};
7use xxhash_rust::xxh3;
8
9#[repr(transparent)]
10#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
11#[serde(transparent)]
12pub struct Hash64(pub u64);
13
14impl From<u64> for Hash64 {
15 fn from(value: u64) -> Self {
16 Hash64(value)
17 }
18}
19
20impl From<Hash64> for u64 {
21 fn from(hash: Hash64) -> Self {
22 hash.0
23 }
24}
25
26impl Hash for Hash64 {
27 fn hash<H: Hasher>(&self, state: &mut H) {
28 state.write_u64(self.0)
29 }
30}
31
32#[repr(transparent)]
33#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
34#[serde(transparent)]
35pub struct Hash128(pub u128);
36
37impl From<u128> for Hash128 {
38 fn from(value: u128) -> Self {
39 Hash128(value)
40 }
41}
42
43impl From<Hash128> for u128 {
44 fn from(hash: Hash128) -> Self {
45 hash.0
46 }
47}
48
49impl Hash for Hash128 {
50 fn hash<H: Hasher>(&self, state: &mut H) {
51 state.write_u128(self.0)
52 }
53}
54
55impl Hash128 {
56 #[inline]
57 pub fn to_hex_string(self) -> String {
58 const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
59 let mut buf = String::with_capacity(32);
60 let val = self.0;
61 for i in (0..32).rev() {
62 let nibble = ((val >> (i * 4)) & 0xf) as usize;
63 buf.push(HEX_DIGITS[nibble] as char);
64 }
65 buf
66 }
67
68 #[inline]
69 pub fn to_hex_string_prefixed(self) -> String {
70 const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
71 let mut buf = String::with_capacity(34);
72 buf.push_str("0x");
73 let val = self.0;
74 for i in (0..32).rev() {
75 let nibble = ((val >> (i * 4)) & 0xf) as usize;
76 buf.push(HEX_DIGITS[nibble] as char);
77 }
78 buf
79 }
80}
81
82#[inline]
83pub fn xxh3_64(data: &[u8]) -> Hash64 {
84 Hash64(xxh3::xxh3_64(data))
85}
86
87#[inline]
88pub fn xxh3_128(data: &[u8]) -> Hash128 {
89 Hash128(xxh3::xxh3_128(data))
90}
91
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn test_xxh3_64() {
98 let data = b"hello world";
99 let hash = xxh3_64(data);
100 assert_eq!(hash, xxh3_64(data));
102 assert_ne!(hash, xxh3_64(b"different data"));
103 }
104
105 #[test]
106 fn test_xxh3_128() {
107 let data = b"hello world";
108 let hash = xxh3_128(data);
109 assert_eq!(hash, xxh3_128(data));
111 assert_ne!(hash, xxh3_128(b"different data"));
112 }
113
114 #[test]
115 fn test_hash64_conversions() {
116 let value: u64 = 12345;
117 let hash = Hash64::from(value);
118 assert_eq!(u64::from(hash), value);
119 }
120
121 #[test]
122 fn test_hash128_conversions() {
123 let value: u128 = 123456789;
124 let hash = Hash128::from(value);
125 assert_eq!(u128::from(hash), value);
126 }
127}