diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index 0358bdc1581..79603a4f438 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -5,7 +5,13 @@ 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) +- 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) - `RngCore` and `SeedableRng` are now part of `rand_core`. (#288) - Add modules to help implementing RNGs `impl` and `le`. (#209, #228) 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 143be3a2d6f..54b122e07b9 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, @@ -253,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 { @@ -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 @@ -420,20 +413,42 @@ 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 } - // 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 -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 74d4e591ed8..fc8c419eeb3 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); @@ -235,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 {