From 15bd4012ba3b41a61ad889129354440202734f84 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 08:49:10 +0200 Subject: [PATCH 1/6] Fix BlockRng64::inner and BlockRng64::inner_mut to match BlockRng --- rand_core/src/impls.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 143be3a2d6f..50878a566d0 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -420,8 +420,13 @@ impl BlockRng64 { } } + /// Return a reference the wrapped `BlockRngCore`. + pub fn inner(&self) -> &R { + &self.core + } + /// Return a mutable reference the wrapped `BlockRngCore`. - pub fn inner(&mut self) -> &mut R { + pub fn inner_mut(&mut self) -> &mut R { &mut self.core } From 8492b75f9a76e0e225d87e1c1683a54b606734dc Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 08:51:41 +0200 Subject: [PATCH 2/6] Provide BlockRng64::index and BlockRng64::generate_and_set --- rand_core/src/impls.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 50878a566d0..c82dc61ffa0 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -430,11 +430,28 @@ impl BlockRng64 { &mut self.core } - // Reset the number of available results. - // This will force a new set of results to be generated on next use. + /// Get the index into the result buffer. + /// + /// If this is equal to or larger than the size of the result buffer then + /// the buffer is "empty" and `generate()` must be called to produce new + /// results. + pub fn index(&self) -> usize { + self.index + } + + /// Reset the number of available results. + /// This will force a new set of results to be generated on next use. pub fn reset(&mut self) { self.index = self.results.as_ref().len(); } + + /// Generate a new set of results immediately, setting the index to the + /// given value. + pub fn generate_and_set(&mut self, index: usize) { + assert!(index < self.results.as_ref().len()); + self.core.generate(&mut self.results); + self.index = index; + } } impl> RngCore for BlockRng64 From 8f3f57ead1c411865e8c63634b89c495c88bd555 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 08:53:38 +0200 Subject: [PATCH 3/6] Update rand_core changelog --- rand_core/CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index 0358bdc1581..ebde39eca60 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -5,7 +5,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [0.1.0] - TODO - date +## [0.2.0] - Unreleased +- Enable the `std` feature by default. (#409) +- Change `BlockRng64::inner` and add `BlockRng64::inner_mut` to mirror `BlockRng`. (#419) +- Add `BlockRng{64}::index` and `BlockRng{64}::generate_and_set`. (#374, #419) + +## [0.1.0] - 2018-04-17 (Split out of the Rand crate, changes here are relative to rand 0.4.2) - `RngCore` and `SeedableRng` are now part of `rand_core`. (#288) - Add modules to help implementing RNGs `impl` and `le`. (#209, #228) From 6c9013def936581c03fc40d43e3f5d26380fb4ea Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 08:54:15 +0200 Subject: [PATCH 4/6] Deny missing docs in rand_core --- rand_core/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 74d4e591ed8..4d2f997967b 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -37,7 +37,9 @@ html_favicon_url = "https://www.rust-lang.org/favicon.ico", html_root_url = "https://docs.rs/rand_core/0.1.0")] +#![deny(missing_docs)] #![deny(missing_debug_implementations)] +#![doc(test(attr(allow(unused_variables), deny(warnings))))] #![cfg_attr(not(feature="std"), no_std)] #![cfg_attr(all(feature="alloc", not(feature="std")), feature(alloc))] @@ -107,6 +109,7 @@ pub mod le; /// A simple example, obviously not generating very *random* output: /// /// ```rust +/// #![allow(dead_code)] /// use rand_core::{RngCore, Error, impls}; /// /// struct CountingRng(u64); From d8da965e71e062e38b7e39d89656c00150b829cc Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 16:02:54 +0200 Subject: [PATCH 5/6] Rely on serde_derive 1.0.38 --- rand_core/Cargo.toml | 2 +- rand_core/src/impls.rs | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index ea438a72f5b..48531094c45 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -25,4 +25,4 @@ serde1 = ["serde", "serde_derive"] # enables serde for BlockRng wrapper [dependencies] serde = { version = "1", optional = true } -serde_derive = { version = "1", optional = true } +serde_derive = { version = "^1.0.38", optional = true } diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index c82dc61ffa0..46166de5b97 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -27,7 +27,6 @@ use core::cmp::min; use core::mem::size_of; use {RngCore, BlockRngCore, CryptoRng, SeedableRng, Error}; -#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; /// Implement `next_u64` via `next_u32`, little-endian order. pub fn next_u64_via_u32(rng: &mut R) -> u64 { @@ -187,9 +186,6 @@ pub fn next_u64_via_fill(rng: &mut R) -> u64 { #[derive(Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct BlockRng { - #[cfg_attr(feature="serde1", serde(bound( - serialize = "R::Results: Serialize", - deserialize = "R::Results: Deserialize<'de>")))] results: R::Results, index: usize, core: R, @@ -386,9 +382,6 @@ impl SeedableRng for BlockRng { #[derive(Clone)] #[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct BlockRng64 { - #[cfg_attr(feature="serde1", serde(bound( - serialize = "R::Results: Serialize", - deserialize = "R::Results: Deserialize<'de>")))] results: R::Results, index: usize, half_used: bool, // true if only half of the previous result is used From 3a8ae170c678caf9d6ebacc04256c1e90631e50d Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sun, 29 Apr 2018 16:10:59 +0200 Subject: [PATCH 6/6] Require AsMut for BlockRngCore::Results --- rand_core/CHANGELOG.md | 1 + rand_core/src/impls.rs | 4 ++-- rand_core/src/lib.rs | 2 +- src/prng/isaac_array.rs | 7 +++++++ src/reseeding.rs | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index ebde39eca60..79603a4f438 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Enable the `std` feature by default. (#409) - Change `BlockRng64::inner` and add `BlockRng64::inner_mut` to mirror `BlockRng`. (#419) - Add `BlockRng{64}::index` and `BlockRng{64}::generate_and_set`. (#374, #419) +- Change `BlockRngCore::Results` bound to also require `AsMut<[Self::Item]>`. (#419) ## [0.1.0] - 2018-04-17 (Split out of the Rand crate, changes here are relative to rand 0.4.2) diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 46166de5b97..54b122e07b9 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -249,7 +249,7 @@ impl BlockRng { } impl> RngCore for BlockRng -where ::Results: AsRef<[u32]> +where ::Results: AsRef<[u32]> + AsMut<[u32]> { #[inline(always)] fn next_u32(&mut self) -> u32 { @@ -448,7 +448,7 @@ impl BlockRng64 { } impl> RngCore for BlockRng64 -where ::Results: AsRef<[u64]> +where ::Results: AsRef<[u64]> + AsMut<[u64]> { #[inline(always)] fn next_u32(&mut self) -> u32 { diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 4d2f997967b..fc8c419eeb3 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -238,7 +238,7 @@ pub trait BlockRngCore { /// Results type. This is the 'block' an RNG implementing `BlockRngCore` /// generates, which will usually be an array like `[u32; 16]`. - type Results: AsRef<[Self::Item]> + Default; + type Results: AsRef<[Self::Item]> + AsMut<[Self::Item]> + Default; /// Generate a new block of results. fn generate(&mut self, results: &mut Self::Results); diff --git a/src/prng/isaac_array.rs b/src/prng/isaac_array.rs index 327cfbf5bcf..3ebf828c684 100644 --- a/src/prng/isaac_array.rs +++ b/src/prng/isaac_array.rs @@ -38,6 +38,13 @@ impl ::core::convert::AsRef<[T]> for IsaacArray { } } +impl ::core::convert::AsMut<[T]> for IsaacArray { + #[inline(always)] + fn as_mut(&mut self) -> &mut [T] { + &mut self.inner[..] + } +} + impl ::core::ops::Deref for IsaacArray { type Target = [T; RAND_SIZE]; #[inline(always)] diff --git a/src/reseeding.rs b/src/reseeding.rs index 0f7f049429a..c74862cbdde 100644 --- a/src/reseeding.rs +++ b/src/reseeding.rs @@ -84,7 +84,7 @@ where R: BlockRngCore + SeedableRng, // implements RngCore, but we can't specify that because ReseedingCore is private impl RngCore for ReseedingRng where R: BlockRngCore + SeedableRng, - ::Results: AsRef<[u32]> + ::Results: AsRef<[u32]> + AsMut<[u32]> { #[inline(always)] fn next_u32(&mut self) -> u32 {