#[cfg(feature = "std")]
use crate::convert::Convert;
use crate::{AHasher};
#[cfg(all(not(feature = "std"), feature = "compile-time-rng"))]
use const_random::const_random;
use core::fmt;
use core::hash::BuildHasher;
use core::hash::Hasher;
#[cfg(feature = "std")]
use lazy_static::*;
use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(feature = "std")]
lazy_static! {
static ref SEEDS: [[u64; 4]; 2] = {
let mut result: [u8; 64] = [0; 64];
getrandom::getrandom(&mut result).expect("getrandom::getrandom() failed.");
result.convert()
};
}
static COUNTER: AtomicUsize = AtomicUsize::new(0);
pub(crate) const PI: [u64; 4] = [
0x243f_6a88_85a3_08d3,
0x1319_8a2e_0370_7344,
0xa409_3822_299f_31d0,
0x082e_fa98_ec4e_6c89,
];
#[cfg(all(not(feature = "std"), not(feature = "compile-time-rng")))]
const PI2: [u64; 4] = [
0x4528_21e6_38d0_1377,
0xbe54_66cf_34e9_0c6c,
0xc0ac_29b7_c97c_50dd,
0x3f84_d5b5_b547_0917,
];
#[inline]
pub(crate) fn seeds() -> [u64; 4] {
#[cfg(feature = "std")]
{ SEEDS[1] }
#[cfg(all(not(feature = "std"), feature = "compile-time-rng"))]
{ [const_random!(u64), const_random!(u64), const_random!(u64), const_random!(u64)] }
#[cfg(all(not(feature = "std"), not(feature = "compile-time-rng")))]
{ PI }
}
#[derive(Clone)]
pub struct RandomState {
pub(crate) k0: u64,
pub(crate) k1: u64,
pub(crate) k2: u64,
pub(crate) k3: u64,
}
impl fmt::Debug for RandomState {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad("RandomState { .. }")
}
}
impl RandomState {
#[inline]
pub fn new() -> RandomState {
#[cfg(feature = "std")]
{
let seeds = *SEEDS;
RandomState::from_keys(seeds[0], seeds[1])
}
#[cfg(all(not(feature = "std"), feature = "compile-time-rng"))]
{
RandomState::from_keys(
[const_random!(u64), const_random!(u64), const_random!(u64), const_random!(u64)],
[const_random!(u64), const_random!(u64), const_random!(u64), const_random!(u64)],
)
}
#[cfg(all(not(feature = "std"), not(feature = "compile-time-rng")))]
{
RandomState::from_keys(PI, PI2)
}
}
#[inline]
pub fn generate_with(k0: u64, k1: u64, k2: u64, k3: u64) -> RandomState {
RandomState::from_keys(seeds(), [k0, k1, k2, k3])
}
fn from_keys(a: [u64; 4], b: [u64; 4]) -> RandomState {
let [k0, k1, k2, k3] = a;
let mut hasher = AHasher::from_random_state(&RandomState { k0, k1, k2, k3 });
let stack_mem_loc = &hasher as *const _ as usize;
#[cfg(not(all(target_arch="arm", target_os="none")))]
{
hasher.write_usize(COUNTER.fetch_add(stack_mem_loc, Ordering::Relaxed));
}
#[cfg(all(target_arch="arm", target_os="none"))]
{
let previous = COUNTER.load(Ordering::Relaxed);
let new = previous.wrapping_add(stack_mem_loc);
COUNTER.store(new, Ordering::Relaxed);
hasher.write_usize(new);
}
#[cfg(all(not(feature = "std"), not(feature = "compile-time-rng")))]
hasher.write_usize(&PI as *const _ as usize);
let mix = |k: u64| {
let mut h = hasher.clone();
h.write_u64(k);
h.finish()
};
RandomState { k0: mix(b[0]), k1: mix(b[1]), k2: mix(b[2]), k3: mix(b[3]) }
}
#[inline]
pub(crate) fn with_fixed_keys() -> RandomState {
let [k0, k1, k2, k3] = seeds();
RandomState { k0, k1, k2, k3 }
}
#[inline]
pub const fn with_seeds(k0: u64, k1: u64, k2: u64, k3: u64) -> RandomState {
RandomState { k0, k1, k2, k3 }
}
}
impl Default for RandomState {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl BuildHasher for RandomState {
type Hasher = AHasher;
#[inline]
fn build_hasher(&self) -> AHasher {
AHasher::from_random_state(self)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_unique() {
let a = RandomState::new();
let b = RandomState::new();
assert_ne!(a.build_hasher().finish(), b.build_hasher().finish());
}
#[test]
fn test_with_seeds_const() {
const _CONST_RANDOM_STATE: RandomState = RandomState::with_seeds(17, 19, 21, 23);
}
}