Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional support for the mem-dbg crate #100

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ serde = { version = "1.0", features = ["derive"], optional = true}
digest = { package = "digest", version = "0.8", default-features = false, optional = true }
digest_0_9 = { package = "digest", version = "0.9", default-features = false, optional = true }
digest_0_10 = { package = "digest", version = "0.10", default-features = false, optional = true }
mem_dbg = {version = "0.2.4", optional=true}

[dev-dependencies]
serde_json = "1.0"
Expand All @@ -30,3 +31,4 @@ serde_json = "1.0"
default = ["std"]
serialize = ["serde"]
std = ["rand"]
mem_dbg = ["dep:mem_dbg", "std"]
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
//! assert_eq!(hash.get(&42), Some(&"the answer"));
//! ```

#![no_std]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

Expand Down Expand Up @@ -72,6 +72,7 @@ pub type XxHash = XxHash64;
/// `RandomXxHashBuilder64` instead.
pub type RandomXxHashBuilder = RandomXxHashBuilder64;

#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
/// An unaligned buffer with iteration support for `UnalignedItem`.
struct UnalignedBuffer<'a, T> {
buf: &'a [u8],
Expand Down
4 changes: 4 additions & 0 deletions src/sixty_four.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub const PRIME_4: u64 = 9_650_029_242_287_828_579;
pub const PRIME_5: u64 = 2_870_177_450_012_600_261;

#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Copy, Clone, PartialEq)]
struct XxCore {
v1: u64,
Expand All @@ -23,6 +24,7 @@ struct XxCore {

/// Calculates the 64-bit hash.
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct XxHash64 {
total_len: u64,
Expand Down Expand Up @@ -125,9 +127,11 @@ impl core::fmt::Debug for XxCore {
#[derive(Debug, Copy, Clone, Default, PartialEq)]
#[repr(align(8))]
#[cfg_attr(feature = "serialize", serde(transparent))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
struct AlignToU64<T>(T);

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
struct Buffer {
#[cfg_attr(feature = "serialize", serde(rename = "buffer"))]
Expand Down
4 changes: 4 additions & 0 deletions src/std_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod sixty_four {
use rand::{self, Rng};

#[derive(Clone)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
/// Constructs a randomized seed and reuses it for multiple hasher instances.
pub struct RandomXxHashBuilder64(u64);

Expand Down Expand Up @@ -34,6 +35,7 @@ pub mod thirty_two {
use rand::{self, Rng};

#[derive(Clone)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
/// Constructs a randomized seed and reuses it for multiple hasher instances. See the usage warning on `XxHash32`.
pub struct RandomXxHashBuilder32(u32);

Expand Down Expand Up @@ -64,6 +66,7 @@ pub mod xxh3 {
use rand::{self, Rng};

#[derive(Clone)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
/// Constructs a randomized seed and reuses it for multiple hasher instances.
pub struct RandomHashBuilder64(u64);

Expand All @@ -88,6 +91,7 @@ pub mod xxh3 {
}

#[derive(Clone)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
/// Constructs a randomized seed and reuses it for multiple hasher instances.
pub struct RandomHashBuilder128(u64);

Expand Down
4 changes: 4 additions & 0 deletions src/thirty_two.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub const PRIME_4: u32 = 668_265_263;
pub const PRIME_5: u32 = 374_761_393;

#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Copy, Clone, PartialEq)]
struct XxCore {
v1: u32,
Expand All @@ -28,6 +29,7 @@ struct XxCore {
/// 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`.
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct XxHash32 {
total_len: u64,
Expand Down Expand Up @@ -115,9 +117,11 @@ impl core::fmt::Debug for XxCore {
#[derive(Debug, Copy, Clone, Default, PartialEq)]
#[repr(align(4))]
#[cfg_attr(feature = "serialize", serde(transparent))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
struct AlignToU32<T>(T);

#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Debug, Copy, Clone, Default, PartialEq)]
struct Buffer {
#[cfg_attr(feature = "serialize", serde(rename = "buffer"))]
Expand Down
9 changes: 9 additions & 0 deletions src/xxh3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pub fn hash128_with_secret(data: &[u8], secret: &[u8]) -> u128 {

/// Calculates the 64-bit hash.
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone, Default)]
pub struct Hash64(State);

Expand Down Expand Up @@ -143,6 +144,7 @@ impl Hasher for Hash64 {

/// Calculates the 128-bit hash.
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone, Default)]
pub struct Hash128(State);

Expand Down Expand Up @@ -203,6 +205,7 @@ const SECRET: Secret = Secret([

#[repr(align(64))]
#[derive(Clone)]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
struct Secret([u8; SECRET_DEFAULT_SIZE]);

const_assert_eq!(mem::size_of::<Secret>() % 16, 0);
Expand Down Expand Up @@ -243,6 +246,7 @@ cfg_if! {
}
}

#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
struct SecretVisitor;

impl<'de> serde::de::Visitor<'de> for SecretVisitor {
Expand Down Expand Up @@ -288,16 +292,19 @@ cfg_if! {
if #[cfg(target_feature = "avx2")] {
#[repr(align(32))]
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone)]
struct Acc([u64; ACC_NB]);
} else if #[cfg(target_feature = "sse2")] {
#[repr(align(16))]
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone)]
struct Acc([u64; ACC_NB]);
} else {
#[repr(align(8))]
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone)]
struct Acc([u64; ACC_NB]);
}
Expand Down Expand Up @@ -848,6 +855,7 @@ const_assert_eq!(INTERNAL_BUFFER_SIZE % STRIPE_LEN, 0);

#[repr(align(64))]
#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone)]
struct State {
acc: Acc,
Expand All @@ -859,6 +867,7 @@ struct State {
}

#[cfg_attr(feature = "serialize", derive(Deserialize, Serialize))]
#[cfg_attr(feature = "mem_dbg", derive(mem_dbg::MemSize, mem_dbg::MemDbg))]
#[derive(Clone)]
enum With {
Default(Secret),
Expand Down