Skip to content

Commit aaaf03f

Browse files
authored
Merge pull request #419 from pitdicker/rand_core_changes
Rand core changes
2 parents a8618f1 + 3a8ae17 commit aaaf03f

File tree

6 files changed

+47
-16
lines changed

6 files changed

+47
-16
lines changed

rand_core/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8-
## [0.1.0] - TODO - date
8+
## [0.2.0] - Unreleased
9+
- Enable the `std` feature by default. (#409)
10+
- Change `BlockRng64::inner` and add `BlockRng64::inner_mut` to mirror `BlockRng`. (#419)
11+
- Add `BlockRng{64}::index` and `BlockRng{64}::generate_and_set`. (#374, #419)
12+
- Change `BlockRngCore::Results` bound to also require `AsMut<[Self::Item]>`. (#419)
13+
14+
## [0.1.0] - 2018-04-17
915
(Split out of the Rand crate, changes here are relative to rand 0.4.2)
1016
- `RngCore` and `SeedableRng` are now part of `rand_core`. (#288)
1117
- Add modules to help implementing RNGs `impl` and `le`. (#209, #228)

rand_core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ serde1 = ["serde", "serde_derive"] # enables serde for BlockRng wrapper
2525

2626
[dependencies]
2727
serde = { version = "1", optional = true }
28-
serde_derive = { version = "1", optional = true }
28+
serde_derive = { version = "^1.0.38", optional = true }

rand_core/src/impls.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use core::cmp::min;
2727
use core::mem::size_of;
2828
use {RngCore, BlockRngCore, CryptoRng, SeedableRng, Error};
2929

30-
#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
3130

3231
/// Implement `next_u64` via `next_u32`, little-endian order.
3332
pub fn next_u64_via_u32<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
@@ -187,9 +186,6 @@ pub fn next_u64_via_fill<R: RngCore + ?Sized>(rng: &mut R) -> u64 {
187186
#[derive(Clone)]
188187
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
189188
pub struct BlockRng<R: BlockRngCore + ?Sized> {
190-
#[cfg_attr(feature="serde1", serde(bound(
191-
serialize = "R::Results: Serialize",
192-
deserialize = "R::Results: Deserialize<'de>")))]
193189
results: R::Results,
194190
index: usize,
195191
core: R,
@@ -253,7 +249,7 @@ impl<R: BlockRngCore> BlockRng<R> {
253249
}
254250

255251
impl<R: BlockRngCore<Item=u32>> RngCore for BlockRng<R>
256-
where <R as BlockRngCore>::Results: AsRef<[u32]>
252+
where <R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
257253
{
258254
#[inline(always)]
259255
fn next_u32(&mut self) -> u32 {
@@ -386,9 +382,6 @@ impl<R: BlockRngCore + SeedableRng> SeedableRng for BlockRng<R> {
386382
#[derive(Clone)]
387383
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
388384
pub struct BlockRng64<R: BlockRngCore + ?Sized> {
389-
#[cfg_attr(feature="serde1", serde(bound(
390-
serialize = "R::Results: Serialize",
391-
deserialize = "R::Results: Deserialize<'de>")))]
392385
results: R::Results,
393386
index: usize,
394387
half_used: bool, // true if only half of the previous result is used
@@ -420,20 +413,42 @@ impl<R: BlockRngCore> BlockRng64<R> {
420413
}
421414
}
422415

416+
/// Return a reference the wrapped `BlockRngCore`.
417+
pub fn inner(&self) -> &R {
418+
&self.core
419+
}
420+
423421
/// Return a mutable reference the wrapped `BlockRngCore`.
424-
pub fn inner(&mut self) -> &mut R {
422+
pub fn inner_mut(&mut self) -> &mut R {
425423
&mut self.core
426424
}
427425

428-
// Reset the number of available results.
429-
// This will force a new set of results to be generated on next use.
426+
/// Get the index into the result buffer.
427+
///
428+
/// If this is equal to or larger than the size of the result buffer then
429+
/// the buffer is "empty" and `generate()` must be called to produce new
430+
/// results.
431+
pub fn index(&self) -> usize {
432+
self.index
433+
}
434+
435+
/// Reset the number of available results.
436+
/// This will force a new set of results to be generated on next use.
430437
pub fn reset(&mut self) {
431438
self.index = self.results.as_ref().len();
432439
}
440+
441+
/// Generate a new set of results immediately, setting the index to the
442+
/// given value.
443+
pub fn generate_and_set(&mut self, index: usize) {
444+
assert!(index < self.results.as_ref().len());
445+
self.core.generate(&mut self.results);
446+
self.index = index;
447+
}
433448
}
434449

435450
impl<R: BlockRngCore<Item=u64>> RngCore for BlockRng64<R>
436-
where <R as BlockRngCore>::Results: AsRef<[u64]>
451+
where <R as BlockRngCore>::Results: AsRef<[u64]> + AsMut<[u64]>
437452
{
438453
#[inline(always)]
439454
fn next_u32(&mut self) -> u32 {

rand_core/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@
3737
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
3838
html_root_url = "https://docs.rs/rand_core/0.1.0")]
3939

40+
#![deny(missing_docs)]
4041
#![deny(missing_debug_implementations)]
42+
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
4143

4244
#![cfg_attr(not(feature="std"), no_std)]
4345
#![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))]
@@ -107,6 +109,7 @@ pub mod le;
107109
/// A simple example, obviously not generating very *random* output:
108110
///
109111
/// ```rust
112+
/// #![allow(dead_code)]
110113
/// use rand_core::{RngCore, Error, impls};
111114
///
112115
/// struct CountingRng(u64);
@@ -235,7 +238,7 @@ pub trait BlockRngCore {
235238

236239
/// Results type. This is the 'block' an RNG implementing `BlockRngCore`
237240
/// generates, which will usually be an array like `[u32; 16]`.
238-
type Results: AsRef<[Self::Item]> + Default;
241+
type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default;
239242

240243
/// Generate a new block of results.
241244
fn generate(&mut self, results: &mut Self::Results);

src/prng/isaac_array.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ impl<T> ::core::convert::AsRef<[T]> for IsaacArray<T> {
3838
}
3939
}
4040

41+
impl<T> ::core::convert::AsMut<[T]> for IsaacArray<T> {
42+
#[inline(always)]
43+
fn as_mut(&mut self) -> &mut [T] {
44+
&mut self.inner[..]
45+
}
46+
}
47+
4148
impl<T> ::core::ops::Deref for IsaacArray<T> {
4249
type Target = [T; RAND_SIZE];
4350
#[inline(always)]

src/reseeding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ where R: BlockRngCore + SeedableRng,
8484
// implements RngCore, but we can't specify that because ReseedingCore is private
8585
impl<R, Rsdr: RngCore> RngCore for ReseedingRng<R, Rsdr>
8686
where R: BlockRngCore<Item = u32> + SeedableRng,
87-
<R as BlockRngCore>::Results: AsRef<[u32]>
87+
<R as BlockRngCore>::Results: AsRef<[u32]> + AsMut<[u32]>
8888
{
8989
#[inline(always)]
9090
fn next_u32(&mut self) -> u32 {

0 commit comments

Comments
 (0)