Skip to content

Commit 0ff7949

Browse files
committed
Some code cleanups
1 parent b6a6083 commit 0ff7949

File tree

3 files changed

+16
-40
lines changed

3 files changed

+16
-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: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,47 @@
11
// Copyright (c) Zefchain Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use rand::{Rng, SeedableRng};
4+
use rand::{
5+
rngs::{SmallRng, ThreadRng},
6+
Rng, SeedableRng,
7+
};
58

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

1114
/// A deterministic RNG.
12-
pub type DeterministicRng = rand::rngs::SmallRng;
15+
pub type DeterministicRng = SmallRng;
1316

1417
/// 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-
}
18+
#[cfg(not(target_arch = "wasm32"))]
19+
pub type NonDeterministicRng = ThreadRng;
20+
#[cfg(target_arch = "wasm32")]
21+
pub type NonDeterministicRng = SmallRng;
3322

3423
/// Returns a deterministic RNG for testing.
3524
pub fn make_deterministic_rng() -> DeterministicRng {
36-
rand::rngs::SmallRng::seed_from_u64(RNG_SEED)
25+
SmallRng::seed_from_u64(RNG_SEED)
3726
}
3827

3928
/// Returns a non-deterministic RNG where supported.
4029
pub fn make_nondeterministic_rng() -> NonDeterministicRng {
4130
#[cfg(target_arch = "wasm32")]
4231
{
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-
)
32+
make_deterministic_rng()
5333
}
54-
5534
#[cfg(not(target_arch = "wasm32"))]
5635
{
57-
NonDeterministicRng(rand::thread_rng())
36+
rand::thread_rng()
5837
}
5938
}
6039

6140
/// Get a random alphanumeric string that can be used for all tests.
6241
pub fn generate_random_alphanumeric_string(length: usize, charset: &[u8]) -> String {
6342
(0..length)
6443
.map(|_| {
65-
let random_index = make_nondeterministic_rng()
66-
.rng_mut()
67-
.gen_range(0..charset.len());
44+
let random_index = make_nondeterministic_rng().gen_range(0..charset.len());
6845
charset[random_index] as char
6946
})
7047
.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)