tinyrand_std/
thread_local.rs1use crate::ClockSeed;
4use core::cell::RefCell;
5use std::rc::Rc;
6use tinyrand::{Rand, Seeded, StdRand};
7
8thread_local! {
9 static THREAD_LOCAL_RAND: Rc<RefCell<StdRand>> = Rc::new(RefCell::new(StdRand::seed(ClockSeed::default().next_u64())));
10}
11
12#[allow(clippy::module_name_repetitions)]
14pub struct ThreadLocalRand(Rc<RefCell<StdRand>>);
15
16impl Rand for ThreadLocalRand {
17 #[inline(always)]
18 fn next_u16(&mut self) -> u16 {
19 self.0.borrow_mut().next_u16()
20 }
21
22 #[inline(always)]
23 fn next_u32(&mut self) -> u32 {
24 self.0.borrow_mut().next_u32()
25 }
26
27 #[inline(always)]
28 fn next_u64(&mut self) -> u64 {
29 self.0.borrow_mut().next_u64()
30 }
31
32 #[inline(always)]
33 fn next_u128(&mut self) -> u128 {
34 self.0.borrow_mut().next_u128()
35 }
36}
37
38pub fn thread_rand() -> ThreadLocalRand {
40 let cell = THREAD_LOCAL_RAND.with(std::clone::Clone::clone);
41 ThreadLocalRand(cell)
42}
43
44#[cfg(test)]
45mod tests;