1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use parking_lot::Mutex;
use rand::rngs::StdRng;
use rand::RngCore;
use rand::SeedableRng;
use std::sync::Arc;
lazy_static::lazy_static! {
static ref FIXT_RNG: FixtRng = {
let seed: u64 = match std::env::var("FIXT_SEED") {
Ok(seed_str) => {
seed_str.parse().expect("Expected integer for FIXT_SEED")
}
Err(std::env::VarError::NotPresent) => { rand::random() },
Err(std::env::VarError::NotUnicode(v)) => { panic!("Invalid FIXT_SEED value: {:?}", v) },
};
println!("Fixturator seed: {}", seed);
FixtRng(Arc::new(
Mutex::new(StdRng::seed_from_u64(seed))
))
};
}
#[derive(Clone)]
pub struct FixtRng(Arc<Mutex<StdRng>>);
impl RngCore for FixtRng {
fn next_u32(&mut self) -> u32 {
self.0.lock().next_u32()
}
fn next_u64(&mut self) -> u64 {
self.0.lock().next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.lock().fill_bytes(dest)
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.0.lock().try_fill_bytes(dest)
}
}
pub fn rng() -> FixtRng {
FIXT_RNG.clone()
}