Skip to content

Commit 3adb3a8

Browse files
committed
Some code cleanups
1 parent b6a6083 commit 3adb3a8

File tree

3 files changed

+12
-30
lines changed

3 files changed

+12
-30
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: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,34 @@
11
// Copyright (c) Zefchain Labs, Inc.
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use rand::{Rng, SeedableRng};
4+
#[cfg(target_arch = "wasm32")]
5+
use std::sync::{Mutex, MutexGuard, OnceLock};
6+
7+
use rand::{rngs::SmallRng, Rng, SeedableRng};
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.
1518
pub struct NonDeterministicRng(
16-
#[cfg(target_arch = "wasm32")] std::sync::MutexGuard<'static, DeterministicRng>,
17-
#[cfg(not(target_arch = "wasm32"))] rand::rngs::ThreadRng,
19+
#[cfg(target_arch = "wasm32")] pub MutexGuard<'static, DeterministicRng>,
20+
#[cfg(not(target_arch = "wasm32"))] pub rand::rngs::ThreadRng,
1821
);
1922

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-
}
33-
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-
4732
static RNG: OnceLock<Mutex<SmallRng>> = OnceLock::new();
4833
NonDeterministicRng(
4934
RNG.get_or_init(|| Mutex::new(make_deterministic_rng()))
@@ -62,9 +47,7 @@ pub fn make_nondeterministic_rng() -> NonDeterministicRng {
6247
pub fn generate_random_alphanumeric_string(length: usize, charset: &[u8]) -> String {
6348
(0..length)
6449
.map(|_| {
65-
let random_index = make_nondeterministic_rng()
66-
.rng_mut()
67-
.gen_range(0..charset.len());
50+
let random_index = make_nondeterministic_rng().0.gen_range(0..charset.len());
6851
charset[random_index] as char
6952
})
7053
.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().0.gen();
2929
bcs::serialize_into(&mut key_prefix, &value).unwrap();
3030
key_prefix
3131
}

0 commit comments

Comments
 (0)