File tree Expand file tree Collapse file tree 1 file changed +9
-15
lines changed Expand file tree Collapse file tree 1 file changed +9
-15
lines changed Original file line number Diff line number Diff line change 11use core:: { alloc:: Layout , convert:: TryInto } ;
2- use rand:: distributions:: { Distribution , Uniform } ;
2+ use rand:: {
3+ distributions:: { Distribution , Uniform } ,
4+ seq:: IteratorRandom ,
5+ } ;
36use rand_chacha:: ChaCha20Rng ;
47use usize_conversions:: IntoUsize ;
58use 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 ;
You can’t perform that action at this time.
0 commit comments