Skip to content

Commit

Permalink
Switch to onceBox
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Kaitchuck <[email protected]>
  • Loading branch information
tkaitchuck committed Oct 25, 2022
1 parent 0c70ae0 commit 904c7db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ proc-macro = true
proc-macro-hack = { version = "0.5.13" }
getrandom = "0.2.0"
tiny-keccak = { version = "2.0.2", features = ["shake"] }
once_cell = { version = "1", default-features = false }
once_cell = { version = "1.15", default-features = false, features = ["race", "alloc"] }
27 changes: 16 additions & 11 deletions macro/src/span.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use proc_macro::Span;
use std::option_env;

use once_cell::sync::Lazy;
use once_cell::race::OnceBox;
use tiny_keccak::{Xof, Hasher, Shake};

static SEED: Lazy<Vec<u8>> = Lazy::new(|| {
if let Some(value) = option_env!("CONST_RANDOM_SEED") {
value.as_bytes().to_vec()
} else {
let mut value = [0u8; 32];
getrandom::getrandom(&mut value).unwrap();
value.to_vec()
}
});

static SEED: OnceBox<Vec<u8>> = OnceBox::new();

fn get_seed() -> &'static [u8] {
&SEED.get_or_init(|| {
if let Some(value) = option_env!("CONST_RANDOM_SEED") {
Box::new(value.as_bytes().to_vec())
} else {
let mut value = [0u8; 32];
getrandom::getrandom(&mut value).unwrap();
Box::new(value.to_vec())
}
})[..]
}

pub(crate) fn gen_random<T: Random>() -> T {
Random::random()
Expand All @@ -29,7 +34,7 @@ pub(crate) trait Random {
fn hash_stuff() -> impl Xof {
let span = Span::call_site();
let mut hasher = Shake::v256();
hasher.update(&*SEED);
hasher.update(get_seed());
hasher.update(&format!("{:?}", span).as_bytes());
hasher
}
Expand Down

0 comments on commit 904c7db

Please sign in to comment.