Skip to content

Commit 2bd4ab3

Browse files
committed
Some code cleanups
1 parent b6a6083 commit 2bd4ab3

File tree

3 files changed

+14
-40
lines changed

3 files changed

+14
-40
lines changed

linera-views/src/common.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,8 @@ impl DeletionSet {
7474
pub(crate) fn get_upper_bound_option(key_prefix: &[u8]) -> Option<Vec<u8>> {
7575
let len = key_prefix.len();
7676
for i in (0..len).rev() {
77-
let val = key_prefix[i];
78-
if val < u8::MAX {
79-
let mut upper_bound = key_prefix[0..i + 1].to_vec();
77+
if key_prefix[i] < u8::MAX {
78+
let mut upper_bound = key_prefix[0..=i].to_vec();
8079
upper_bound[i] += 1;
8180
return Some(upper_bound);
8281
}

linera-views/src/random.rs

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,45 @@
11
// Copyright (c) Zefchain Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use rand::{Rng, SeedableRng};
4+
use rand::{rngs::SmallRng, Rng, SeedableRng};
55

66
// The following seed is chosen to have equal numbers of 1s and 0s, as advised by
77
// https://docs.rs/rand/latest/rand/rngs/struct.SmallRng.html
88
// Specifically, it's "01" × 32 in binary
99
const RNG_SEED: u64 = 6148914691236517205;
1010

1111
/// A deterministic RNG.
12-
pub type DeterministicRng = rand::rngs::SmallRng;
12+
pub type DeterministicRng = SmallRng;
1313

1414
/// 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;
3320

3421
/// Returns a deterministic RNG for testing.
3522
pub fn make_deterministic_rng() -> DeterministicRng {
36-
rand::rngs::SmallRng::seed_from_u64(RNG_SEED)
23+
SmallRng::seed_from_u64(RNG_SEED)
3724
}
3825

3926
/// Returns a non-deterministic RNG where supported.
4027
pub fn make_nondeterministic_rng() -> NonDeterministicRng {
4128
#[cfg(target_arch = "wasm32")]
4229
{
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()
5331
}
54-
5532
#[cfg(not(target_arch = "wasm32"))]
5633
{
57-
NonDeterministicRng(rand::thread_rng())
34+
rand::thread_rng()
5835
}
5936
}
6037

6138
/// Get a random alphanumeric string that can be used for all tests.
6239
pub fn generate_random_alphanumeric_string(length: usize, charset: &[u8]) -> String {
6340
(0..length)
6441
.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());
6843
charset[random_index] as char
6944
})
7045
.collect()

linera-views/src/test_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
/// Returns a random key prefix used for tests
2626
pub fn get_random_key_prefix() -> Vec<u8> {
2727
let mut key_prefix = vec![0];
28-
let value: usize = make_nondeterministic_rng().rng_mut().gen();
28+
let value: usize = make_nondeterministic_rng().gen();
2929
bcs::serialize_into(&mut key_prefix, &value).unwrap();
3030
key_prefix
3131
}

0 commit comments

Comments
 (0)