Skip to content

Commit 719f1d2

Browse files
committed
use IteratorRandom::choose
1 parent 4f86dc0 commit 719f1d2

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/binary/level_4_entries.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use core::{alloc::Layout, convert::TryInto};
2-
use rand::distributions::{Distribution, Uniform};
2+
use rand::{
3+
distributions::{Distribution, Uniform},
4+
seq::IteratorRandom,
5+
};
36
use rand_chacha::ChaCha20Rng;
47
use usize_conversions::IntoUsize;
58
use x86_64::{
@@ -121,23 +124,14 @@ impl UsedLevel4Entries {
121124
.map(|(idx, _)| idx);
122125

123126
// Choose the free entry index.
124-
let idx = if let Some(rng) = self.rng.as_mut() {
125-
// Count the entries and randomly choose an index in `[0..count)`.
126-
let count = free_entries.clone().count();
127-
if count == 0 {
128-
panic!("no usable level 4 entries found")
129-
}
130-
let distribution = Uniform::from(0..count);
131-
let idx = distribution.sample(rng);
132-
133-
// Get the index of the free entry.
134-
free_entries.nth(idx).unwrap()
127+
let idx_opt = if let Some(rng) = self.rng.as_mut() {
128+
// Randomly choose an index.
129+
free_entries.choose(rng)
135130
} else {
136131
// Choose the first index.
137-
free_entries
138-
.next()
139-
.expect("no usable level 4 entries found")
132+
free_entries.next()
140133
};
134+
let idx = idx_opt.expect("no usable level 4 entry found");
141135

142136
// Mark the entry as used.
143137
self.entry_state[idx] = true;

0 commit comments

Comments
 (0)