### Rust example using const_xxh3 and xxh3 Source: https://docs.rs/xxhash-rust/0.8.15/index This example demonstrates the usage of the `xxhash-rust` crate in Rust. It shows how to use both the constant evaluation version (`const_xxh3`) for compile-time hashing and the runtime version (`xxh3_64`) for hashing string inputs. It includes assertions to verify the hashing results. ```rust use xxhash_rust::const_xxh3::xxh3_64 as const_xxh3; use xxhash_rust::xxh3::xxh3_64; const TEST: u64 = const_xxh3(b"TEST"); fn test_input(text: &str) -> bool { match xxh3_64(text.as_bytes()) { TEST => true, _ => false } } assert!(!test_input("tEST")); assert!(test_input("TEST")); ``` -------------------------------- ### Rust Example: Using xxhash-rust for Hashing Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/index This example demonstrates how to use the xxhash-rust crate to calculate a 64-bit xxHash (xxh3) of a string. It shows the usage of both the runtime and compile-time constant versions of the hashing function. The code requires the 'xxh3' and 'const_xxh3' features to be enabled. ```rust use xxhash_rust::const_xxh3::xxh3_64 as const_xxh3; use xxhash_rust::xxh3::xxh3_64; const TEST: u64 = const_xxh3(b"TEST"); fn test_input(text: &str) -> bool { match xxh3_64(text.as_bytes()) { TEST => true, _ => false } } assert!(!test_input("tEST")); assert!(test_input("TEST")); ``` -------------------------------- ### Default Implementation for Xxh64 in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Provides a default implementation for `Xxh64` using `Default::default()`. This creates a new `Xxh64` hasher with a seed of 0, using the `Xxh64Builder`. This is a convenient way to get a hasher instance without explicitly configuring a builder. ```rust impl Default for Xxh64 { #[inline(always)] fn default() -> Self { Xxh64Builder::new(0).build() } } ``` -------------------------------- ### Clone Implementation for Xxh64Builder Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Provides the ability to create a duplicate of an Xxh64Builder instance. This is useful for reusing builder configurations or performing multiple hashing operations with the same initial setup. ```rust fn clone(&self) -> Xxh64Builder fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Define const `INITIAL_ACC` for XXH3 accumulators in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Defines a constant `INITIAL_ACC` of type `Acc` with initial values derived from prime numbers. These initial values are used to initialize the accumulators in the XXH3 hashing process, providing a starting point for the hash calculation. ```rust const INITIAL_ACC: Acc = Acc([ xxh32::PRIME_3 as u64, xxh64::PRIME_1, xxh64::PRIME_2, xxh64::PRIME_3, xxh64::PRIME_4, xxh32::PRIME_2 as u64, xxh64::PRIME_5, xxh32::PRIME_1 as u64 ]); ``` -------------------------------- ### Build Xxh64 Hasher from Builder Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Consumes the Xxh64Builder instance and returns a configured Xxh64 hasher. This is the final step in preparing a hasher for use after setting any necessary configuration through the builder. ```rust pub const fn build(self) -> Xxh64 ``` -------------------------------- ### Xxh64Builder API Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Documentation for the Xxh64Builder struct, which serves as a hash builder for Xxh64. ```APIDOC ## Struct Xxh64Builder ### Description Hash builder for `Xxh64`. ### Methods #### `new(seed: u64) -> Self` Creates builder with provided `seed`. #### `build(self) -> Xxh64` Creates hasher. ### Trait Implementations #### `BuildHasher` - `type Hasher = Xxh64` Type of the hasher that will be created. - `fn build_hasher(&self) -> Self::Hasher` Creates a new hasher. - `fn hash_one(&self, x: T) -> u64` where T: Hash, Self: Sized, Self::Hasher: Hasher Calculates the hash of a single value. #### `Clone` - `fn clone(&self) -> Xxh64Builder` Returns a duplicate of the value. - `fn clone_from(&mut self, source: &Self)` Performs copy-assignment from `source`. #### `Default` - `fn default() -> Xxh64Builder` Returns the “default value” for a type. ### Auto Trait Implementations - `Freeze` - `RefUnwindSafe` - `Send` - `Sync` - `Unpin` - `UnwindSafe` ### Blanket Implementations - `Any` - `Borrow` - `BorrowMut` - `CloneToUninit` - `From` - `Into` - `TryFrom` - `TryInto` ``` -------------------------------- ### Public xxh3_128 Hashing Function in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 The primary public interface for generating a 128-bit xxHash. It takes only the input data and uses default settings for seed and secret. This is the simplest way to get a hash value. Dependencies: `xxh3_128_internal`, `DEFAULT_SECRET`, and `xxh3_128_long_default`. ```rust ///Returns 128bit hash for provided input. pub fn xxh3_128(input: &[u8]) -> u128 { xxh3_128_internal(input, 0, &DEFAULT_SECRET, xxh3_128_long_default) } ``` -------------------------------- ### Xxh32 Hashing Algorithm Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh32/struct Provides documentation for the Xxh32 struct, which implements the XXH32 streaming algorithm. It includes details on creating a new hasher, updating it with input data, finalizing the hash, and resetting the state. ```APIDOC ## Struct Xxh32 ### Description XXH32 Streaming algorithm ### Methods #### `new(seed: u32) -> Self` Creates a new hasher with the specified seed. - **seed** (u32) - The seed value for the hasher. #### `update(&mut self, input: &[u8])` Hashes the provided input data. - **input** (&[u8]) - A slice of bytes to be hashed. #### `digest(&self) -> u32` Finalizes the hashing process and returns the resulting 32-bit hash. Returns: - **u32** - The calculated hash value. #### `reset(&mut self, seed: u32)` Resets the hasher's state with a new seed value. - **seed** (u32) - The new seed value to initialize the hasher with. ``` -------------------------------- ### xxh3_64_long_impl: Final hash calculation for long inputs in xxhash-rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/const_xxh3 This function computes the final 64-bit xxhash for long inputs. It calls `hash_long_internal_loop` to get the accumulated values and then uses `merge_accs` to combine them into the final hash. It incorporates the input length and a prime multiplier. ```rust const fn xxh3_64_long_impl(input: &[u8], secret: &[u8]) -> u64 { let acc = hash_long_internal_loop(input, secret); merge_accs(&acc, secret, SECRET_MERGEACCS_START, (input.len() as u64).wrapping_mul(xxh64::PRIME_1)) } ``` -------------------------------- ### Xxh3 Hasher Initialization Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Provides methods for creating new Xxh3 hasher instances. Includes constructors for default settings, custom operations (seed and secret), custom secrets only, and custom seeds. These methods allow users to configure the hashing process based on their specific needs. ```rust impl Xxh3 { #[inline(always)] ///Creates new hasher with default settings pub const fn new() -> Self { Self::with_custom_ops(0, DEFAULT_SECRET) } #[inline] ///Creates new hasher with all options. const fn with_custom_ops(seed: u64, secret: [u8; DEFAULT_SECRET_SIZE]) -> Self { Self { acc: INITIAL_ACC, custom_secret: Aligned64(secret), buffer: Aligned64([mem::MaybeUninit::uninit(); INTERNAL_BUFFER_SIZE]), buffered_size: 0, nb_stripes_acc: 0, total_len: 0, seed, } } #[inline(always)] ///Creates new hasher with custom seed. pub const fn with_secret(secret: [u8; DEFAULT_SECRET_SIZE]) -> Self { Self::with_custom_ops(0, secret) } #[inline(always)] ///Creates new hasher with custom seed. pub fn with_seed(seed: u64) -> Self { Self::with_custom_ops(seed, custom_default_secret(seed)) } } ``` -------------------------------- ### Get Unaligned Chunk (Rust) Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/utils Reads an unaligned chunk of data of type T from a byte slice at a given offset. This function is useful when data alignment is not guaranteed. It uses `ptr::read_unaligned` for safe reading of potentially misaligned data, with assertions for data integrity. ```Rust 20#[inline(always)] 21pub fn get_unaligned_chunk(input: &[u8], offset: usize) -> T { 22 debug_assert!(mem::size_of::() > 0); //Size MUST be positive 23 debug_assert!(mem::size_of::() <= input.len().saturating_sub(offset)); //Must fit 24 25 unsafe { 26 ptr::read_unaligned(input.as_ptr().add(offset) as *const T) 27 } 28} ``` -------------------------------- ### Build Xxh3Default Hasher Instance Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/struct Implements the `build` method for Xxh3DefaultBuilder, which consumes the builder and returns an instance of the `Xxh3Default` hasher. This is a typical builder pattern usage. ```rust pub const fn build(self) -> Xxh3Default ``` -------------------------------- ### xxh64.rs: Function to initialize accumulator registers Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 The `init_v` function initializes the four accumulator registers (`v`) used in the xxHash algorithm. It takes a `seed` value and applies a series of additions and subtractions with predefined primes (`PRIME_1`, `PRIME_2`) to set the initial state. This function is marked as `const fn` allowing it to be used in compile-time contexts. ```rust #[inline(always)] const fn init_v(seed: u64) -> (u64, u64, u64, u64) { ( seed.wrapping_add(PRIME_1).wrapping_add(PRIME_2), seed.wrapping_add(PRIME_2), seed, seed.wrapping_sub(PRIME_1), ) } ``` -------------------------------- ### Rust Cargo.toml dependency for xxhash-rust Source: https://docs.rs/xxhash-rust/0.8.15/index This snippet shows how to add the `xxhash-rust` crate as a dependency in your `Cargo.toml` file. It demonstrates enabling specific features like `xxh3` and `const_xxh3` for customized functionality. ```toml [dependencies.xxhash-rust] version = "0.8.5" features = ["xxh3", "const_xxh3"] ``` -------------------------------- ### Get Aligned Chunk Reference (Rust) Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/utils Retrieves a reference to an aligned chunk of data of type T from a byte slice at a given offset. It includes assertions to ensure the size of T is positive and fits within the remaining slice. This function is intended for performance-critical sections and uses unsafe code for direct memory access. ```Rust 1//! Utilities of the crate 2use core::{ptr, mem}; 3 4#[inline(always)] 5pub const fn get_aligned_chunk_ref(input: &[u8], offset: usize) -> &T { 6 debug_assert!(mem::size_of::() > 0); //Size MUST be positive 7 debug_assert!(mem::size_of::() <= input.len().saturating_sub(offset)); //Must fit 8 9 unsafe { 10 &*(input.as_ptr().add(offset) as *const T) 11 } 12} 13 14#[allow(unused)] 15#[inline(always)] 16pub const fn get_aligned_chunk(input: &[u8], offset: usize) -> T { 17 *get_aligned_chunk_ref(input, offset) 18} ``` -------------------------------- ### Define inline function `read_64le_unaligned` for unaligned 64-bit little-endian reads in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Defines an inline function `read_64le_unaligned` that reads a 64-bit unsigned integer from a byte slice at a given offset, assuming little-endian byte order. It uses `get_aligned_chunk_ref` to get a reference to an aligned chunk and converts it to a 64-bit integer. ```rust #[inline(always)] fn read_64le_unaligned(data: &[u8], offset: usize) -> u64 { u64::from_ne_bytes(*get_aligned_chunk_ref(data, offset)).to_le() } ``` -------------------------------- ### xxh32.rs: Helper functions and macros for xxHash32 Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh32 This section contains helper functions and macros used in the xxHash32 implementation. This includes `finalize` for processing the last few bytes, `init_v` for initializing internal state, and `round_loop` for iterating over data chunks during hashing. ```rust fn finalize(mut input: u32, mut data: &[u8], is_aligned: bool) -> u32 { while data.len() >= 4 { input = input.wrapping_add(match is_aligned { true => get_aligned_chunk::(data, 0).to_le().wrapping_mul(PRIME_3), false => get_unaligned_chunk::(data, 0).to_le().wrapping_mul(PRIME_3), }); input = input.rotate_left(17).wrapping_mul(PRIME_4); data = &data[4..]; } for byte in data.iter() { input = input.wrapping_add((*byte as u32).wrapping_mul(PRIME_5)); input = input.rotate_left(11).wrapping_mul(PRIME_1); } avalanche(input) } #[inline(always)] const fn init_v(seed: u32) -> (u32, u32, u32, u32) { ( seed.wrapping_add(PRIME_1).wrapping_add(PRIME_2), seed.wrapping_add(PRIME_2), seed, seed.wrapping_sub(PRIME_1), ) } macro_rules! round_loop { ($input:ident => $($v:tt)+) => { $($v)+.0 = round($($v)+.0, get_unaligned_chunk::($input, 0).to_le()); $($v)+.1 = round($($v)+.1, get_unaligned_chunk::($input, 4).to_le()); $($v)+.2 = round($($v)+.2, get_unaligned_chunk::($input, 8).to_le()); $($v)+.3 = round($($v)+.3, get_unaligned_chunk::($input, 12).to_le()); $input = &$input[16..]; } } ``` -------------------------------- ### Define inline function `read_32le_unaligned` for unaligned 32-bit little-endian reads in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Defines an inline function `read_32le_unaligned` that reads a 32-bit unsigned integer from a byte slice at a given offset, assuming little-endian byte order. It uses `get_aligned_chunk_ref` to get a reference to an aligned chunk and converts it to a 32-bit integer. ```rust #[inline(always)] fn read_32le_unaligned(data: &[u8], offset: usize) -> u32 { u32::from_ne_bytes(*get_aligned_chunk_ref(data, offset)).to_le() } ``` -------------------------------- ### Create New Xxh3DefaultBuilder Instance Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/struct Provides a constant function `new()` to create a new instance of Xxh3DefaultBuilder with default parameters. This is a common pattern for builder types. ```rust pub const fn new() -> Self ``` -------------------------------- ### Xxh64 State Reset with Seed in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Resets the internal state of the Xxh64 hasher to its initial values, using a provided seed. This allows the hasher to be reused for new hashing operations with a different starting point. It resets the total length, initializes the internal vector 'v' with the seed, and clears the memory size. ```rust #[inline] ///Resets state with provided seed. pub fn reset(&mut self, seed: u64) { self.total_len = 0; self.v = init_v(seed); self.mem_size = 0; } ``` -------------------------------- ### Rust Cargo.toml: Enabling xxhash-rust Features Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/index This snippet shows how to configure the xxhash-rust dependency in a Cargo.toml file. It specifically enables the 'xxh3' and 'const_xxh3' features, which are required for using the xxh3 hashing algorithm and its compile-time constant version, respectively. ```toml [dependencies.xxhash-rust] version = "0.8.5" features = ["xxh3", "const_xxh3"] ``` -------------------------------- ### Rust: Implement Default for Xxh32 Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh32 Provides a default implementation for the Xxh32 hasher, initializing it with a seed of 0. This allows creating a default Xxh32 instance using `Xxh32::default()`. ```rust impl Default for Xxh32 { #[inline(always)] fn default() -> Self { Self::new(0) } } ``` -------------------------------- ### Xxh3DefaultBuilder Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/struct Provides a hash builder for Xxh3Default, allowing for the creation of Xxh3 hashers. ```APIDOC ## Struct Xxh3DefaultBuilder ### Description Hash builder for `Xxh3Default`. ### Methods #### `new()` ```rust pub const fn new() -> Self ``` Creates a new instance with default parameters. #### `build()` ```rust pub const fn build(self) -> Xxh3Default ``` Creates an `Xxh3` instance. ### Trait Implementations #### `BuildHasher` ##### `Hasher` Type ```rust type Hasher = Xxh3Default; ``` ##### `build_hasher()` ```rust fn build_hasher(&self) -> Self::Hasher; ``` Creates a new hasher. ##### `hash_one()` ```rust fn hash_one(&self, x: T) -> u64 where T: Hash, Self: Sized, Self::Hasher: Hasher; ``` Calculates the hash of a single value. #### `Clone` ##### `clone()` ```rust fn clone(&self) -> Xxh3DefaultBuilder; ``` Returns a duplicate of the value. ##### `clone_from()` ```rust fn clone_from(&mut self, source: &Self); ``` Performs copy-assignment from `source`. #### `Default` ##### `default()` ```rust fn default() -> Self; ``` Returns the default value for the type. #### `Copy` (This type implements the `Copy` trait, indicating it can be copied by value.) ``` -------------------------------- ### Xxh64Builder for Creating Xxh64 Hasher in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Defines a builder struct `Xxh64Builder` to configure and create `Xxh64` hashers. It holds a seed value and provides methods to create a builder with a specific seed (`new`) and to build the `Xxh64` instance (`build`). This follows the builder pattern for flexible hasher instantiation. ```rust #[derive(Clone, Copy, Default)] ///Hash builder for `Xxh64` pub struct Xxh64Builder { seed: u64 } impl Xxh64Builder { #[inline(always)] ///Creates builder with provided `seed` pub const fn new(seed: u64) -> Self { Self { seed } } #[inline(always)] ///Creates hasher. pub const fn build(self) -> Xxh64 { Xxh64::new(self.seed) } } ``` -------------------------------- ### Implementing BuildHasher Trait for Xxh64Builder in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Implements the `core::hash::BuildHasher` trait for `Xxh64Builder`. This allows `Xxh64Builder` to be used in contexts that require a hasher builder, such as creating hash maps. The `build_hasher` method returns a new `Xxh64` hasher instance configured by the builder. ```rust impl core::hash::BuildHasher for Xxh64Builder { type Hasher = Xxh64; #[inline(always)] fn build_hasher(&self) -> Self::Hasher { self.build() } } ``` -------------------------------- ### xxh64.rs: 64-bit xxHash direct hashing function Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Calculates the 64-bit xxHash for a given byte slice and seed. It handles inputs smaller than the chunk size and larger inputs by processing them in chunks using the `round_loop` macro and finalizing with `finalize`. Dependencies include `crate::utils` and `crate::xxh64_common`. ```rust ///Returns hash for the provided input. pub fn xxh64(mut input: &[u8], seed: u64) -> u64 { let input_len = input.len() as u64; let mut result; if input.len() >= CHUNK_SIZE { let mut v = init_v(seed); loop { round_loop!(input => v); if input.len() < CHUNK_SIZE { break; } } result = v.0.rotate_left(1).wrapping_add(v.1.rotate_left(7)) .wrapping_add(v.2.rotate_left(12)) .wrapping_add(v.3.rotate_left(18)); result = merge_round(result, v.0); result = merge_round(result, v.1); result = merge_round(result, v.2); result = merge_round(result, v.3); } else { result = seed.wrapping_add(PRIME_5) } result = result.wrapping_add(input_len); finalize(result, input, false) } ``` -------------------------------- ### Create Xxh64Builder with Seed Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct This function creates a new Xxh64Builder instance, initializing it with a specific seed value. The seed is crucial for hash computations and allows for reproducible hashing or different hash outputs for the same data. ```rust pub const fn new(seed: u64) -> Self ``` -------------------------------- ### xxh64.rs: 64-bit xxHash streaming implementation Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Provides a streaming API for calculating the 64-bit xxHash. The `Xxh64` struct maintains the internal state (total length, accumulator registers `v`, and a temporary buffer `mem`). The `update` method processes input chunks, filling the buffer or applying rounds as needed. It depends on `init_v`, `round`, and `CHUNK_SIZE` from `xxh64_common` and `utils`. ```rust ///XXH64 Streaming algorithm #[derive(Clone)] pub struct Xxh64 { total_len: u64, v: (u64, u64, u64, u64), mem: [u64; 4], mem_size: u64, } impl Xxh64 { #[inline] ///Creates new state with provided seed. pub const fn new(seed: u64) -> Self { Self { total_len: 0, v: init_v(seed), mem: [0, 0, 0, 0], mem_size: 0, } } ///Adds chunk of data to hash. pub fn update(&mut self, mut input: &[u8]) { self.total_len = self.total_len.wrapping_add(input.len() as u64); if (self.mem_size as usize + input.len()) < CHUNK_SIZE { Buffer { ptr: self.mem.as_mut_ptr() as *mut u8, len: mem::size_of_val(&self.mem), offset: self.mem_size as _, }.copy_from_slice(input); self.mem_size += input.len() as u64; return } if self.mem_size > 0 { //previous if can fail only when we do not have enough space in buffer for input. //hence fill_len >= input.len() let fill_len = CHUNK_SIZE - self.mem_size as usize; Buffer { ptr: self.mem.as_mut_ptr() as *mut u8, len: mem::size_of_val(&self.mem), offset: self.mem_size as _, }.copy_from_slice_by_size(input, fill_len); self.v.0 = round(self.v.0, self.mem[0].to_le()); self.v.1 = round(self.v.1, self.mem[1].to_le()); self.v.2 = round(self.v.2, self.mem[2].to_le()); self.v.3 = round(self.v.3, self.mem[3].to_le()); input = &input[fill_len..]; self.mem_size = 0; } if input.len() >= CHUNK_SIZE { loop { round_loop!(input => self.v); if input.len() < CHUNK_SIZE { ``` -------------------------------- ### Xxh3Builder for Custom Xxh3 Hashing in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Provides a builder pattern for creating `Xxh3` hashers with custom seeds and secrets. It includes methods to set the seed and secret, and a `build` method to construct the `Xxh3` instance. This builder also implements `core::hash::BuildHasher`. ```rust impl Xxh3Builder { #[inline(always)] ///Creates new instance with default params. pub const fn new() -> Self { Self { seed: 0, secret: DEFAULT_SECRET, } } #[inline(always)] ///Sets `seed` for `xxh3` algorithm pub const fn with_seed(mut self, seed: u64) -> Self { self.seed = seed; self } #[inline(always)] ///Sets custom `secret` for `xxh3` algorithm pub const fn with_secret(mut self, secret: [u8; DEFAULT_SECRET_SIZE]) -> Self { self.secret = secret; self } #[inline(always)] ///Creates `Xxh3` instance pub const fn build(self) -> Xxh3 { Xxh3::with_custom_ops(self.seed, self.secret) } } impl core::hash::BuildHasher for Xxh3Builder { type Hasher = Xxh3; #[inline(always)] fn build_hasher(&self) -> Self::Hasher { self.build() } } ``` -------------------------------- ### XXH32 Hashing Implementation in Rust Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh32/struct Provides the XXH32 streaming algorithm implementation. It allows creating a new hasher with a seed, updating it with input data, finalizing the hash digest, and resetting the state with a new seed. It implements Clone and Default traits. ```rust pub struct Xxh32 { /* private fields */ } impl Xxh32 { pub const fn new(seed: u32) -> Self; pub fn update(&mut self, input: &[u8]); pub fn digest(&self) -> u32; pub fn reset(&mut self, seed: u32); } impl Clone for Xxh32 { fn clone(&self) -> Xxh32; fn clone_from(&mut self, source: &Self); } impl Default for Xxh32 { fn default() -> Self; } ``` -------------------------------- ### BuildHasher Trait Implementation for Xxh64Builder Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Implements the BuildHasher trait for Xxh64Builder, defining the associated Hasher type and methods for creating and using the hasher. This allows Xxh64Builder to be used in contexts requiring a generic hasher builder. ```rust type Hasher = Xxh64 fn build_hasher(&self) -> Self::Hasher ``` -------------------------------- ### XXH3Default Struct Initialization and Reset Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 Defines the Xxh3Default struct for XXH3 streaming hashing with default settings. Includes a const fn `new` for creating a hasher instance and a `reset` method to reinitialize its state. ```rust pub struct Xxh3Default { acc: Acc, buffer: Aligned64<[mem::MaybeUninit; INTERNAL_BUFFER_SIZE]>, buffered_size: u16, nb_stripes_acc: usize, total_len: u64, } impl Xxh3Default { const DEFAULT_SECRET: Aligned64<[u8; DEFAULT_SECRET_SIZE]> = Aligned64(DEFAULT_SECRET); #[inline(always)] ///Creates new hasher with default settings pub const fn new() -> Self { Self { acc: INITIAL_ACC, buffer: Aligned64([mem::MaybeUninit::uninit(); INTERNAL_BUFFER_SIZE]), buffered_size: 0, nb_stripes_acc: 0, total_len: 0, } } #[inline(always)] ///Resets state pub fn reset(&mut self) { self.acc = INITIAL_ACC; self.total_len = 0; self.buffered_size = 0; self.nb_stripes_acc = 0; } } ``` -------------------------------- ### Implementing Hasher Trait for Xxh64 in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Implements the `core::hash::Hasher` trait for the `Xxh64` struct, providing `finish` and `write` methods. The `finish` method simply calls the `digest` method, and the `write` method calls the `update` method. This allows `Xxh64` to be used with Rust's standard hashing infrastructure. ```rust impl core::hash::Hasher for Xxh64 { #[inline(always)] fn finish(&self) -> u64 { self.digest() } #[inline(always)] fn write(&mut self, input: &[u8]) { self.update(input) } } ``` -------------------------------- ### Implementing std::io::Write for Xxh64 in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 Implements the `std::io::Write` trait for the `Xxh64` struct. This allows the hasher to be used as a writer, accepting data directly into its update mechanism. The `write` method updates the hasher with the provided buffer, and `flush` is a no-op as hashing is typically incremental. ```rust #[cfg(feature = "std")] impl std::io::Write for Xxh64 { #[inline] fn write(&mut self, buf: &[u8]) -> std::io::Result { self.update(buf); Ok(buf.len()) } #[inline] fn flush(&mut self) -> std::io::Result<()> { Ok(()) } } ``` -------------------------------- ### Xxh3DefaultBuilder for Default Xxh3 Hashing in Rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh3 A simple builder for creating a default `Xxh3` hasher instance. It implements `core::hash::BuildHasher` and provides a `build` method to instantiate `Xxh3Default`. This is useful for scenarios requiring a standard `Xxh3` hash with default parameters. ```rust impl Xxh3DefaultBuilder { #[inline(always)] ///Creates new instance with default params. pub const fn new() -> Self { Self } #[inline(always)] ///Creates `Xxh3` instance pub const fn build(self) -> Xxh3Default { Xxh3Default::new() } } impl core::hash::BuildHasher for Xxh3DefaultBuilder { type Hasher = Xxh3Default; #[inline(always)] fn build_hasher(&self) -> Self::Hasher { self.build() } } ``` -------------------------------- ### XXH3 64-bit Hash for 7-128 Bytes (Rust) Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/const_xxh3 Initializes the XXH3 64-bit hash calculation for input data between 7 and 128 bytes. It sets up an initial accumulator value based on the input length and primes. ```rust const fn xxh3_64_7to128(input: &[u8], seed: u64, secret: &[u8]) -> u64 { let mut acc = (input.len() as u64).wrapping_mul(xxh64::PRIME_1); if input.len() > 32 { if input.len() > 64 { if input.len() > 96 { acc = acc.wrapping_add(mix16_b(input, 48, secret, 96, seed)); acc = acc.wrapping_add(mix16_b(input, input.len()-64, secret, 112, seed)); } ``` -------------------------------- ### Calculate 64-bit xxHash with Seed (Rust) Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/fn Calculates a 64-bit xxHash for the given input data using a specified seed. For optimal performance, consider pre-generating secrets at compile time using `const_custom_default_secret` from `const_xxh3`. ```Rust pub fn xxh3_64_with_seed(input: &[u8], seed: u64) -> u64 ``` -------------------------------- ### Rust: xxHash64 Hashing Function Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/const_xxh64 Implements the const 64-bit xxHash algorithm. It takes a byte slice and a seed as input and returns the 64-bit hash. Handles inputs larger than CHUNK_SIZE by processing in chunks and then finalizing the hash. ```Rust use core::mem; use crate::xxh64_common::*; #[inline(always)] const fn read_u32(input: &[u8], cursor: usize) -> u32 { input[cursor] as u32 | (input[cursor + 1] as u32) << 8 | (input[cursor + 2] as u32) << 16 | (input[cursor + 3] as u32) << 24 } #[inline(always)] const fn read_u64(input: &[u8], cursor: usize) -> u64 { input[cursor] as u64 | (input[cursor + 1] as u64) << 8 | (input[cursor + 2] as u64) << 16 | (input[cursor + 3] as u64) << 24 | (input[cursor + 4] as u64) << 32 | (input[cursor + 5] as u64) << 40 | (input[cursor + 6] as u64) << 48 | (input[cursor + 7] as u64) << 56 } const fn finalize(mut input: u64, data: &[u8], mut cursor: usize) -> u64 { let mut len = data.len() - cursor; while len >= 8 { input ^= round(0, read_u64(data, cursor)); cursor += mem::size_of::(); len -= mem::size_of::(); input = input.rotate_left(27).wrapping_mul(PRIME_1).wrapping_add(PRIME_4) } if len >= 4 { input ^= (read_u32(data, cursor) as u64).wrapping_mul(PRIME_1); cursor += mem::size_of::(); len -= mem::size_of::(); input = input.rotate_left(23).wrapping_mul(PRIME_2).wrapping_add(PRIME_3); } while len > 0 { input ^= (data[cursor] as u64).wrapping_mul(PRIME_5); cursor += mem::size_of::(); len -= mem::size_of::(); input = input.rotate_left(11).wrapping_mul(PRIME_1); } avalanche(input) } ///Returns hash for the provided input. pub const fn xxh64(input: &[u8], seed: u64) -> u64 { let input_len = input.len() as u64; let mut cursor = 0; let mut result; if input.len() >= CHUNK_SIZE { let mut v1 = seed.wrapping_add(PRIME_1).wrapping_add(PRIME_2); let mut v2 = seed.wrapping_add(PRIME_2); let mut v3 = seed; let mut v4 = seed.wrapping_sub(PRIME_1); loop { v1 = round(v1, read_u64(input, cursor)); cursor += mem::size_of::(); v2 = round(v2, read_u64(input, cursor)); cursor += mem::size_of::(); v3 = round(v3, read_u64(input, cursor)); cursor += mem::size_of::(); v4 = round(v4, read_u64(input, cursor)); cursor += mem::size_of::(); if (input.len() - cursor) < CHUNK_SIZE { break; } } result = v1.rotate_left(1).wrapping_add(v2.rotate_left(7)) .wrapping_add(v3.rotate_left(12)) .wrapping_add(v4.rotate_left(18)); result = merge_round(result, v1); result = merge_round(result, v2); result = merge_round(result, v3); result = merge_round(result, v4); } else { result = seed.wrapping_add(PRIME_5) } result = result.wrapping_add(input_len); finalize(result, input, cursor) } ``` -------------------------------- ### Xxh64Builder Struct Definition Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Defines the Xxh64Builder struct, which acts as a builder for creating Xxh64 hashers. It contains private fields and is used to configure hashing parameters before creating the hasher instance. ```rust pub struct Xxh64Builder { /* private fields */ } ``` -------------------------------- ### Xxh3DefaultBuilder Default Trait Implementation Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/struct Implements the `Default` trait for `Xxh3DefaultBuilder`, providing a `default()` method to create a builder with its default configuration. This simplifies initialization. ```rust fn default() -> Self ``` -------------------------------- ### xxh64.rs: Macro for processing 32-byte chunks Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh64 The `round_loop` macro simplifies the repetitive processing of 32-byte chunks of input data within the xxHash algorithm. It takes an input slice and a tuple of accumulator registers `v`, applies the `round` function to each 64-bit segment of the chunk, and advances the input slice pointer. This macro is used in both the direct `xxh64` function and the `Xxh64::update` method. ```rust macro_rules! round_loop { ($input:ident => $($v:tt)+) => { $($v)+.0 = round($($v)+.0, get_unaligned_chunk::($input, 0).to_le()); $($v)+.1 = round($($v)+.1, get_unaligned_chunk::($input, 8).to_le()); $($v)+.2 = round($($v)+.2, get_unaligned_chunk::($input, 16).to_le()); $($v)+.3 = round($($v)+.3, get_unaligned_chunk::($input, 24).to_le()); $input = &$input[32..]; } } ``` -------------------------------- ### Default Implementation for Xxh64Builder Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh64/struct Implements the Default trait for Xxh64Builder, providing a default builder instance. This typically uses a default seed value, allowing for quick initialization when specific seed values are not required. ```rust fn default() -> Xxh64Builder ``` -------------------------------- ### Rust: Implement std::io::Write for Xxh32 Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh32 Implements the `std::io::Write` trait for the Xxh32 hasher, enabling it to be used with standard Rust I/O operations. The `write` method updates the hash with the provided buffer, and `flush` is a no-op as the hasher is always ready. ```rust impl std::io::Write for Xxh32 { #[inline] fn write(&mut self, buf: &[u8]) -> std::io::Result { self.update(buf); Ok(buf.len()) } #[inline] fn flush(&mut self) -> std::io::Result<()> { Ok(()) } } ``` -------------------------------- ### Xxh3DefaultBuilder BuildHasher Trait Implementation Source: https://docs.rs/xxhash-rust/0.8.15/xxhash_rust/xxh3/struct Implements the `BuildHasher` trait for `Xxh3DefaultBuilder`. This includes defining the associated `Hasher` type and providing the `build_hasher` method to create hasher instances. ```rust type Hasher = Xxh3Default; fn build_hasher(&self) -> Self::Hasher ``` -------------------------------- ### XXH32 Hashing Constants and Functions (Rust) Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/xxh32_common This Rust code defines constants such as CHUNK_SIZE, PRIME_1 through PRIME_5, and provides inline functions 'round' and 'avalanche' for the XXH32 hashing algorithm. These are essential for the core hashing logic. ```rust #![allow(unused)] use core::mem; pub const CHUNK_SIZE: usize = mem::size_of::() * 4; pub const PRIME_1: u32 = 0x9E3779B1; pub const PRIME_2: u32 = 0x85EBCA77; pub const PRIME_3: u32 = 0xC2B2AE3D; pub const PRIME_4: u32 = 0x27D4EB2F; pub const PRIME_5: u32 = 0x165667B1; #[inline] pub const fn round(acc: u32, input: u32) -> u32 { acc.wrapping_add(input.wrapping_mul(PRIME_2)) .rotate_left(13) .wrapping_mul(PRIME_1) } #[inline] pub const fn avalanche(mut input: u32) -> u32 { input ^= input >> 15; input = input.wrapping_mul(PRIME_2); input ^= input >> 13; input = input.wrapping_mul(PRIME_3); input ^= input >> 16; input } ``` -------------------------------- ### xxh3_64_7to128: Hash medium inputs (7-128 bytes) with xxhash-rust Source: https://docs.rs/xxhash-rust/0.8.15/src/xxhash_rust/const_xxh3 This function computes a 64-bit xxhash for input data ranging from 7 to 128 bytes. It employs a loop-based accumulation strategy using `mix16_b` and concludes with an `avalanche` call for the final hash. It takes the input slice, a seed, and the secret key. ```rust const fn xxh3_64_7to128(input: &[u8], seed: u64, secret: &[u8]) -> u64 { let mut acc = (input.len() as u64).wrapping_mul(xxh64::PRIME_1); const NB_LANES: usize = 8; let mut idx = 0; while idx < NB_LANES { acc = acc.wrapping_add(mix16_b(input, 16*idx, secret, 16*idx, seed)); idx += 1; } acc = avalanche(acc); idx = 0; while idx < NB_LANES { acc = acc.wrapping_add(mix16_b(input, 16*idx, secret, 16*idx, seed)); idx += 1; } acc = acc.wrapping_add(mix16_b(input, input.len()-16, secret, 16*(NB_LANES-1), seed)); avalanche(acc) } ```