Skip to content

Commit

Permalink
Move from lazy-static to once-cell
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Kaitchuck <Tom.Kaitchuck@gmail.com>
  • Loading branch information
tkaitchuck committed Jan 12, 2021
1 parent b175b79 commit c828cc6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -64,7 +64,7 @@ codegen-units = 1
version_check = "0.9"

[target.'cfg(any(target_os = "linux", target_os = "android", target_os = "windows", target_os = "macos", target_os = "ios", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", target_os = "dragonfly", target_os = "solaris", target_os = "illumos", target_os = "fuchsia", target_os = "redox", target_os = "cloudabi", target_os = "haiku", target_os = "vxworks", target_os = "emscripten", target_os = "wasi"))'.dependencies]
lazy_static = { version = "1.4.0" }
once_cell = { version = "1.4.0", default-features = false, features = ["unstable", "alloc"] }
getrandom = { version = "0.2.0" }
const-random = { version = "0.1.12", optional = true }
serde = { version = "1.0.117", optional = true }
Expand Down
28 changes: 16 additions & 12 deletions src/random_state.rs
Expand Up @@ -7,17 +7,11 @@ use core::fmt;
use core::hash::BuildHasher;
use core::hash::Hasher;
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
use lazy_static::*;
use once_cell::race::OnceBox;
use core::sync::atomic::{AtomicUsize, Ordering};

#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
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 SEEDS: OnceBox<[[u64; 4]; 2]> = OnceBox::new();

static COUNTER: AtomicUsize = AtomicUsize::new(0);

Expand All @@ -39,11 +33,17 @@ const PI2: [u64; 4] = [
#[inline]
pub(crate) fn seeds() -> [u64; 4] {
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
{ SEEDS[1] }
{
SEEDS.get_or_init(|| {
let mut result: [u8; 64] = [0; 64];
getrandom::getrandom(&mut result).expect("getrandom::getrandom() failed.");
Box::new(result.convert())
})[1]
}
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
{ [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 = "runtime-rng"), not(feature = "compile-time-rng")))]
{ PI }
{ PI }
}


Expand Down Expand Up @@ -74,7 +74,11 @@ impl RandomState {
pub fn new() -> RandomState {
#[cfg(all(feature = "runtime-rng", not(all(feature = "compile-time-rng", test))))]
{
let seeds = *SEEDS;
let seeds = SEEDS.get_or_init(|| {
let mut result: [u8; 64] = [0; 64];
getrandom::getrandom(&mut result).expect("getrandom::getrandom() failed.");
Box::new(result.convert())
});
RandomState::from_keys(seeds[0], seeds[1])
}
#[cfg(all(feature = "compile-time-rng", any(not(feature = "runtime-rng"), test)))]
Expand Down

0 comments on commit c828cc6

Please sign in to comment.