Skip to content

Commit

Permalink
Implement Clone for the hashers andstates
Browse files Browse the repository at this point in the history
  • Loading branch information
shepmaster committed Oct 16, 2024
1 parent b4ee2a5 commit 0b28c59
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/xxhash32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Bytes = [u8; 16];

const BYTES_IN_LANE: usize = mem::size_of::<Bytes>();

#[derive(PartialEq)]
#[derive(Clone, PartialEq)]
struct BufferData(Lanes);

impl BufferData {
Expand All @@ -48,7 +48,7 @@ impl fmt::Debug for BufferData {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
struct Buffer {
offset: usize,
data: BufferData,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Buffer {
}
}

#[derive(PartialEq)]
#[derive(Clone, PartialEq)]
struct Accumulators(Lanes);

impl Accumulators {
Expand Down Expand Up @@ -199,7 +199,7 @@ impl fmt::Debug for Accumulators {
/// Although this struct implements [`hash::Hasher`][], it only calculates a
/// 32-bit number, leaving the upper bits as 0. This means it is
/// unlikely to be correct to use this in places like a [`HashMap`][std::collections::HashMap].
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Hasher {
seed: u32,
accumulators: Accumulators,
Expand Down Expand Up @@ -362,6 +362,7 @@ const fn round(mut acc: u32, lane: u32) -> u32 {

/// Constructs [`Hasher`][] for multiple hasher instances. See
/// the [usage warning][Hasher#caution].
#[derive(Clone)]
pub struct State(u32);

impl State {
Expand Down Expand Up @@ -389,6 +390,12 @@ mod test {

use super::*;

const _TRAITS: () = {
const fn is_clone<T: Clone>() {}
is_clone::<Hasher>();
is_clone::<State>();
};

const EMPTY_BYTES: [u8; 0] = [];

#[test]
Expand Down Expand Up @@ -505,6 +512,7 @@ mod random_impl {

/// Constructs a randomized seed and reuses it for multiple hasher
/// instances. See the [usage warning][Hasher#caution].
#[derive(Clone)]
pub struct RandomState(State);

impl Default for RandomState {
Expand Down Expand Up @@ -533,6 +541,12 @@ mod random_impl {

use super::*;

const _: () = {
const fn is_clone<T: Clone>() {}
is_clone::<Hasher>();
is_clone::<RandomState>();
};

#[test]
fn can_be_used_in_a_hashmap_with_a_random_seed() {
let mut hash: HashMap<_, _, RandomState> = Default::default();
Expand Down
8 changes: 8 additions & 0 deletions src/xxhash3_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const DEFAULT_SECRET_RAW: DefaultSecret = [
const DEFAULT_SECRET: &Secret = unsafe { Secret::new_unchecked(&DEFAULT_SECRET_RAW) };

/// Calculates the 64-bit hash.
#[derive(Clone)]
pub struct Hasher {
#[cfg(feature = "alloc")]
inner: with_alloc::AllocRawHasher,
Expand Down Expand Up @@ -184,6 +185,7 @@ unsafe impl<const N: usize> FixedMutBuffer for &mut [u8; N] {}

/// Holds secret and temporary buffers that are ensured to be
/// appropriately sized.
#[derive(Clone)]
pub struct SecretBuffer<S> {
seed: u64,
secret: S,
Expand Down Expand Up @@ -516,6 +518,7 @@ impl StripeAccumulator {
/// usages may desire more flexibility. This type, combined with
/// [`SecretBuffer`][], offer that flexibility at the cost of a
/// generic type.
#[derive(Clone)]
pub struct RawHasher<S> {
secret_buffer: SecretBuffer<S>,
buffer_usage: usize,
Expand Down Expand Up @@ -1331,6 +1334,11 @@ mod test {

use super::*;

const _: () = {
const fn is_clone<T: Clone>() {}
is_clone::<Hasher>();
};

const EMPTY_BYTES: [u8; 0] = [];

#[test]
Expand Down
21 changes: 17 additions & 4 deletions src/xxhash64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Bytes = [u8; 32];

const BYTES_IN_LANE: usize = mem::size_of::<Bytes>();

#[derive(PartialEq)]
#[derive(Clone, PartialEq)]
struct BufferData(Lanes);

impl BufferData {
Expand All @@ -48,7 +48,7 @@ impl fmt::Debug for BufferData {
}
}

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
struct Buffer {
offset: usize,
data: BufferData,
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Buffer {
}
}

#[derive(PartialEq)]
#[derive(Clone, PartialEq)]
struct Accumulators(Lanes);

impl Accumulators {
Expand Down Expand Up @@ -210,7 +210,7 @@ impl fmt::Debug for Accumulators {
}

/// Calculates the 64-bit hash.
#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
pub struct Hasher {
seed: u64,
accumulators: Accumulators,
Expand Down Expand Up @@ -373,6 +373,7 @@ const fn round(mut acc: u64, lane: u64) -> u64 {
}

/// Constructs [`Hasher`][] for multiple hasher instances.
#[derive(Clone)]
pub struct State(u64);

impl State {
Expand Down Expand Up @@ -400,6 +401,12 @@ mod test {

use super::*;

const _TRAITS: () = {
const fn is_clone<T: Clone>() {}
is_clone::<Hasher>();
is_clone::<State>();
};

const EMPTY_BYTES: [u8; 0] = [];

#[test]
Expand Down Expand Up @@ -494,6 +501,7 @@ mod random_impl {

/// Constructs a randomized seed and reuses it for multiple hasher
/// instances.
#[derive(Clone)]
pub struct RandomState(State);

impl Default for RandomState {
Expand Down Expand Up @@ -522,6 +530,11 @@ mod random_impl {

use super::*;

const _TRAITS: () = {
const fn is_clone<T: Clone>() {}
is_clone::<RandomState>();
};

#[test]
fn can_be_used_in_a_hashmap_with_a_random_seed() {
let mut hash: HashMap<_, _, RandomState> = Default::default();
Expand Down

0 comments on commit 0b28c59

Please sign in to comment.