Skip to main content

http_compress/xx_hash3_64/
impl.rs

1use super::*;
2
3/// Default construction for [`XxHash3_64`].
4impl Default for XxHash3_64 {
5    /// Builds a hasher without initializing unused streaming or prefix storage.
6    ///
7    /// # Returns
8    ///
9    /// - `Self` - The allocation-free hasher.
10    #[inline(always)]
11    fn default() -> Self {
12        Self {
13            inner: None,
14            prefix: [const { MaybeUninit::uninit() }; HASH_PREFIX_CAPACITY],
15            prefix_len: 0,
16        }
17    }
18}
19
20/// Clone implementation for [`XxHash3_64`].
21impl Clone for XxHash3_64 {
22    /// Clones the hasher while copying only the initialized prefix bytes.
23    ///
24    /// # Returns
25    ///
26    /// - `Self` - A cloned hasher with identical state.
27    #[inline(always)]
28    fn clone(&self) -> Self {
29        let Self {
30            inner,
31            prefix: src_prefix,
32            prefix_len,
33        } = self;
34        let mut prefix: [MaybeUninit<u8>; HASH_PREFIX_CAPACITY] =
35            [const { MaybeUninit::uninit() }; HASH_PREFIX_CAPACITY];
36        let len: usize = *prefix_len;
37        if inner.is_none() && len > 0 {
38            // SAFETY: `0..len` was initialized in `src_prefix` and `len <= HASH_PREFIX_CAPACITY`.
39            unsafe {
40                ptr::copy_nonoverlapping(src_prefix.as_ptr(), prefix.as_mut_ptr(), len);
41            }
42        }
43        Self {
44            inner: inner.clone(),
45            prefix,
46            prefix_len: len,
47        }
48    }
49}
50
51/// Implements `impl core::hash::Hasher for XxHash3_64`.
52impl core::hash::Hasher for XxHash3_64 {
53    /// Feeds bytes into the same XXH3 stream while deferring large-state setup.
54    ///
55    /// # Arguments
56    ///
57    /// - `&[u8]` - The bytes to hash.
58    #[inline(always)]
59    fn write(&mut self, bytes: &[u8]) {
60        let Self {
61            inner,
62            prefix,
63            prefix_len,
64        } = self;
65        if let Some(hasher) = inner {
66            hasher.write(bytes);
67            return;
68        }
69        let current_len: usize = *prefix_len;
70        let incoming_len: usize = bytes.len();
71        if incoming_len <= HASH_PREFIX_CAPACITY - current_len {
72            // SAFETY: `current_len + incoming_len <= HASH_PREFIX_CAPACITY` and
73            // `MaybeUninit<u8>` has identical layout to `u8`.
74            unsafe {
75                ptr::copy_nonoverlapping(
76                    bytes.as_ptr(),
77                    prefix.as_mut_ptr().add(current_len).cast::<u8>(),
78                    incoming_len,
79                );
80            }
81            *prefix_len = current_len + incoming_len;
82            return;
83        }
84        let mut hasher: RawHasher<&'static [u8; DEFAULT_SECRET_LENGTH]> =
85            RawHasher::new(SecretBuffer::default());
86        // SAFETY: `0..current_len` bytes in `prefix` were initialized by prior writes.
87        let initialized: &[u8] =
88            unsafe { slice::from_raw_parts(prefix.as_ptr().cast::<u8>(), current_len) };
89        hasher.write(initialized);
90        hasher.write(bytes);
91        *inner = Some(hasher);
92    }
93
94    /// Feeds a single byte into the uninitialized prefix buffer without slice copy overhead.
95    ///
96    /// # Arguments
97    ///
98    /// - `u8` - The byte to hash.
99    #[inline(always)]
100    fn write_u8(&mut self, value: u8) {
101        let Self {
102            inner,
103            prefix,
104            prefix_len,
105        } = self;
106        if let Some(hasher) = inner {
107            hasher.write_u8(value);
108            return;
109        }
110        let current_len: usize = *prefix_len;
111        if current_len < HASH_PREFIX_CAPACITY {
112            prefix[current_len].write(value);
113            *prefix_len = current_len + 1;
114            return;
115        }
116        let mut hasher: RawHasher<&'static [u8; DEFAULT_SECRET_LENGTH]> =
117            RawHasher::new(SecretBuffer::default());
118        // SAFETY: `0..current_len` bytes in `prefix` were initialized by prior writes.
119        let initialized: &[u8] =
120            unsafe { slice::from_raw_parts(prefix.as_ptr().cast::<u8>(), current_len) };
121        hasher.write(initialized);
122        hasher.write_u8(value);
123        *inner = Some(hasher);
124    }
125
126    /// Reports the same hash irrespective of write boundaries or prefix overflow.
127    ///
128    /// # Returns
129    ///
130    /// - `u64` - The hash of all bytes written so far.
131    #[inline(always)]
132    fn finish(&self) -> u64 {
133        let Self {
134            inner,
135            prefix,
136            prefix_len,
137        } = self;
138        match inner {
139            Some(hasher) => hasher.finish(),
140            None => {
141                // SAFETY: `0..*prefix_len` bytes in `prefix` were initialized by prior writes.
142                let initialized: &[u8] =
143                    unsafe { slice::from_raw_parts(prefix.as_ptr().cast::<u8>(), *prefix_len) };
144                xxhash3_64::Hasher::oneshot(initialized)
145            }
146        }
147    }
148}