@@ -219,20 +219,38 @@ impl<R: BlockRngCore> BlockRng<R> {
219
219
}
220
220
221
221
/// Return a reference the wrapped `BlockRngCore`.
222
+ #[ inline( always) ]
222
223
pub fn inner ( & self ) -> & R {
223
224
& self . core
224
225
}
225
226
226
227
/// Return a mutable reference the wrapped `BlockRngCore`.
228
+ #[ inline( always) ]
227
229
pub fn inner_mut ( & mut self ) -> & mut R {
228
230
& mut self . core
229
231
}
230
-
231
- // Reset the number of available results.
232
- // This will force a new set of results to be generated on next use.
232
+
233
+ /// Get the index into the result buffer
234
+ #[ inline( always) ]
235
+ pub fn index ( & self ) -> usize {
236
+ self . index
237
+ }
238
+
239
+ /// Reset the number of available results.
240
+ /// This will force a new set of results to be generated on next use.
241
+ #[ inline( always) ]
233
242
pub fn reset ( & mut self ) {
234
243
self . index = self . results . as_ref ( ) . len ( ) ;
235
244
}
245
+
246
+ /// Generate a new set of results immediately, setting the index to the
247
+ /// given value.
248
+ #[ inline( always) ]
249
+ pub fn generate ( & mut self , index : usize ) {
250
+ assert ! ( index < self . results. as_ref( ) . len( ) ) ;
251
+ self . core . generate ( & mut self . results ) ;
252
+ self . index = index;
253
+ }
236
254
}
237
255
238
256
impl < R : BlockRngCore < Item =u32 > > RngCore for BlockRng < R >
@@ -241,8 +259,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]>
241
259
#[ inline( always) ]
242
260
fn next_u32 ( & mut self ) -> u32 {
243
261
if self . index >= self . results . as_ref ( ) . len ( ) {
244
- self . core . generate ( & mut self . results ) ;
245
- self . index = 0 ;
262
+ self . generate ( 0 ) ;
246
263
}
247
264
248
265
let value = self . results . as_ref ( ) [ self . index ] ;
@@ -271,13 +288,11 @@ where <R as BlockRngCore>::Results: AsRef<[u32]>
271
288
// Read an u64 from the current index
272
289
read_u64 ( self . results . as_ref ( ) , index)
273
290
} else if index >= len {
274
- self . core . generate ( & mut self . results ) ;
275
- self . index = 2 ;
291
+ self . generate ( 2 ) ;
276
292
read_u64 ( self . results . as_ref ( ) , 0 )
277
293
} else {
278
294
let x = u64:: from ( self . results . as_ref ( ) [ len-1 ] ) ;
279
- self . core . generate ( & mut self . results ) ;
280
- self . index = 1 ;
295
+ self . generate ( 1 ) ;
281
296
let y = u64:: from ( self . results . as_ref ( ) [ 0 ] ) ;
282
297
( y << 32 ) | x
283
298
}
@@ -329,8 +344,7 @@ where <R as BlockRngCore>::Results: AsRef<[u32]>
329
344
let mut read_len = 0 ;
330
345
while read_len < dest. len ( ) {
331
346
if self . index >= self . results . as_ref ( ) . len ( ) {
332
- self . core . generate ( & mut self . results ) ;
333
- self . index = 0 ;
347
+ self . generate ( 0 ) ;
334
348
}
335
349
let ( consumed_u32, filled_u8) =
336
350
fill_via_u32_chunks ( & self . results . as_ref ( ) [ self . index ..] ,
0 commit comments