Skip to content

Commit

Permalink
use const_random
Browse files Browse the repository at this point in the history
  • Loading branch information
notsatvrn committed Jan 2, 2024
1 parent 7afadf7 commit 6883e61
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bench-md = []
bench-plot = []

[dependencies]
const-random = "0.1"
rand_chacha = { version = "0.3", default_features = false }
rand_core = "0.6"

Expand Down Expand Up @@ -71,4 +72,4 @@ harness = false

[[bench]]
name = "quality"
harness = false
harness = false
19 changes: 0 additions & 19 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,6 @@ extern crate rustc_version;
use rustc_version::{version_meta, Channel};

fn main() {
// generate seed for GxBuildHasher RNG if entropy isn't available
let entropy = cfg!(target_feature = "rdrand") || cfg!(feature = "std");
if entropy {
println!("cargo:rustc-cfg=entropy")
} else {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let seed_path = std::path::Path::new(&out_dir).join("seed.rs");

use rand::RngCore;
let mut seed = [0u8; 32];
let mut rng = rand::thread_rng();
rng.fill_bytes(&mut seed);

let mut string = "const SEED: [u8; 32] = [".to_owned();
string.push_str(&seed.into_iter().map(|i| i.to_string()).collect::<Vec<String>>().join(","));
string.push_str("];");
std::fs::write(seed_path, &string).unwrap();
}

if version_meta().unwrap().channel == Channel::Nightly
&& cfg!(target_arch = "x86_64")
&& cfg!(target_feature = "avx2")
Expand Down
5 changes: 1 addition & 4 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,14 @@ impl Hasher for GxHasher {
#[derive(Clone, Debug)]
pub struct GxBuildHasher(State);

#[cfg(not(entropy))]
include!(concat!(env!("OUT_DIR"), "/seed.rs"));

impl Default for GxBuildHasher {
#[inline]
fn default() -> GxBuildHasher {
let mut uninit: MaybeUninit<State> = MaybeUninit::uninit();
#[cfg(entopy)]
let mut rng = rand_chacha::ChaCha12Rng::from_entropy();
#[cfg(not(entropy))]
let mut rng = rand_chacha::ChaCha12Rng::from_seed(SEED);
let mut rng = rand_chacha::ChaCha12Rng::from_seed(const_random::const_random!([u8; 32]));
unsafe {
let ptr = uninit.as_mut_ptr() as *mut u8;
let slice = core::slice::from_raw_parts_mut(ptr, VECTOR_SIZE);
Expand Down

0 comments on commit 6883e61

Please sign in to comment.