|
1 | 1 | // Copyright (c) Zefchain Labs, Inc.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -use rand::{Rng, SeedableRng}; |
| 4 | +use rand::{rngs::SmallRng, Rng, SeedableRng}; |
5 | 5 |
|
6 | 6 | // The following seed is chosen to have equal numbers of 1s and 0s, as advised by
|
7 | 7 | // https://docs.rs/rand/latest/rand/rngs/struct.SmallRng.html
|
8 | 8 | // Specifically, it's "01" × 32 in binary
|
9 | 9 | const RNG_SEED: u64 = 6148914691236517205;
|
10 | 10 |
|
11 | 11 | /// A deterministic RNG.
|
12 |
| -pub type DeterministicRng = rand::rngs::SmallRng; |
| 12 | +pub type DeterministicRng = SmallRng; |
13 | 13 |
|
14 | 14 | /// A RNG that is non-deterministic if the platform supports it.
|
15 |
| -pub struct NonDeterministicRng( |
16 |
| - #[cfg(target_arch = "wasm32")] std::sync::MutexGuard<'static, DeterministicRng>, |
17 |
| - #[cfg(not(target_arch = "wasm32"))] rand::rngs::ThreadRng, |
18 |
| -); |
19 |
| - |
20 |
| -impl NonDeterministicRng { |
21 |
| - /// Access the internal RNG. |
22 |
| - pub fn rng_mut(&mut self) -> &mut impl Rng { |
23 |
| - #[cfg(target_arch = "wasm32")] |
24 |
| - { |
25 |
| - &mut *self.0 |
26 |
| - } |
27 |
| - #[cfg(not(target_arch = "wasm32"))] |
28 |
| - { |
29 |
| - &mut self.0 |
30 |
| - } |
31 |
| - } |
32 |
| -} |
| 15 | +#[cfg(not(target_arch = "wasm32"))] |
| 16 | +pub type NonDeterministicRng = rand::rngs::ThreadRng; |
| 17 | +/// A RNG that is non-deterministic if the platform supports it. |
| 18 | +#[cfg(target_arch = "wasm32")] |
| 19 | +pub type NonDeterministicRng = SmallRng; |
33 | 20 |
|
34 | 21 | /// Returns a deterministic RNG for testing.
|
35 | 22 | pub fn make_deterministic_rng() -> DeterministicRng {
|
36 |
| - rand::rngs::SmallRng::seed_from_u64(RNG_SEED) |
| 23 | + SmallRng::seed_from_u64(RNG_SEED) |
37 | 24 | }
|
38 | 25 |
|
39 | 26 | /// Returns a non-deterministic RNG where supported.
|
40 | 27 | pub fn make_nondeterministic_rng() -> NonDeterministicRng {
|
41 | 28 | #[cfg(target_arch = "wasm32")]
|
42 | 29 | {
|
43 |
| - use std::sync::{Mutex, OnceLock}; |
44 |
| - |
45 |
| - use rand::rngs::SmallRng; |
46 |
| - |
47 |
| - static RNG: OnceLock<Mutex<SmallRng>> = OnceLock::new(); |
48 |
| - NonDeterministicRng( |
49 |
| - RNG.get_or_init(|| Mutex::new(make_deterministic_rng())) |
50 |
| - .lock() |
51 |
| - .expect("failed to lock RNG mutex"), |
52 |
| - ) |
| 30 | + make_deterministic_rng() |
53 | 31 | }
|
54 |
| - |
55 | 32 | #[cfg(not(target_arch = "wasm32"))]
|
56 | 33 | {
|
57 |
| - NonDeterministicRng(rand::thread_rng()) |
| 34 | + rand::thread_rng() |
58 | 35 | }
|
59 | 36 | }
|
60 | 37 |
|
61 | 38 | /// Get a random alphanumeric string that can be used for all tests.
|
62 | 39 | pub fn generate_random_alphanumeric_string(length: usize, charset: &[u8]) -> String {
|
63 | 40 | (0..length)
|
64 | 41 | .map(|_| {
|
65 |
| - let random_index = make_nondeterministic_rng() |
66 |
| - .rng_mut() |
67 |
| - .gen_range(0..charset.len()); |
| 42 | + let random_index = make_nondeterministic_rng().gen_range(0..charset.len()); |
68 | 43 | charset[random_index] as char
|
69 | 44 | })
|
70 | 45 | .collect()
|
|
0 commit comments