Skip to content

Commit

Permalink
Reuse std::hash::RandomState to get rid of rand dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ogxd committed May 27, 2024
1 parent 41f8ff1 commit 579e579
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gxhash"
authors = ["Olivier Giniaux"]
version = "3.2.0"
version = "3.3.0"
edition = "2021"
description = "GxHash non-cryptographic algorithm"
license = "MIT"
Expand All @@ -20,9 +20,10 @@ bench-plot = []
hybrid = []

[dependencies]
rand = "0.8"
# No dependencies!

[dev-dependencies]
rand = "0.8"
lazy_static = { version = "1.4" }
itertools = "0.12.0"
# Benchmarks
Expand Down
12 changes: 3 additions & 9 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hasher};
use std::mem::MaybeUninit;

use rand::RngCore;

use crate::gxhash::platform::*;
use crate::gxhash::*;
Expand Down Expand Up @@ -138,13 +135,10 @@ pub struct GxBuildHasher(State);
impl Default for GxBuildHasher {
#[inline]
fn default() -> GxBuildHasher {
let mut uninit: MaybeUninit<State> = MaybeUninit::uninit();
let mut rng = rand::thread_rng();
let random_state = std::hash::RandomState::new();
unsafe {
let ptr = uninit.as_mut_ptr() as *mut u8;
let slice = std::slice::from_raw_parts_mut(ptr, VECTOR_SIZE);
rng.fill_bytes(slice);
GxBuildHasher(uninit.assume_init())
let state: State = std::mem::transmute(random_state);
GxBuildHasher(state)
}
}
}
Expand Down

0 comments on commit 579e579

Please sign in to comment.