From 1a3e41fe905ab119b07a40b0b440a91e53a55bc7 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Sun, 10 Mar 2019 11:09:28 +1100 Subject: [PATCH 01/10] Add '/' to all documents on github.io --- Cargo.toml | 2 +- rand_chacha/Cargo.toml | 2 +- rand_core/Cargo.toml | 2 +- rand_hc/Cargo.toml | 2 +- rand_isaac/Cargo.toml | 2 +- rand_pcg/Cargo.toml | 2 +- rand_xorshift/Cargo.toml | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ccdc008cec7..6907dfa5732 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand" +documentation = "https://rust-random.github.io/rand/" homepage = "https://crates.io/crates/rand" description = """ Random number generators and other randomness functionality. diff --git a/rand_chacha/Cargo.toml b/rand_chacha/Cargo.toml index f8cbe7f9ac1..dbb2331f372 100644 --- a/rand_chacha/Cargo.toml +++ b/rand_chacha/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_chacha" +documentation = "https://rust-random.github.io/rand/rand_chacha/" homepage = "https://crates.io/crates/rand_chacha" description = """ ChaCha random number generator diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index 340fb3a40a5..98e47ddf899 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_core" +documentation = "https://rust-random.github.io/rand/rand_core/" homepage = "https://crates.io/crates/rand_core" description = """ Core random number generator traits and tools for implementation. diff --git a/rand_hc/Cargo.toml b/rand_hc/Cargo.toml index b4aa08ecfa0..8195ea9b733 100644 --- a/rand_hc/Cargo.toml +++ b/rand_hc/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_hc" +documentation = "https://rust-random.github.io/rand/rand_hc/" homepage = "https://crates.io/crates/rand_hc" description = """ HC128 random number generator diff --git a/rand_isaac/Cargo.toml b/rand_isaac/Cargo.toml index 16d0cab905a..3624db80d18 100644 --- a/rand_isaac/Cargo.toml +++ b/rand_isaac/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_isaac" +documentation = "https://rust-random.github.io/rand/rand_isaac/" homepage = "https://crates.io/crates/rand_isaac" description = """ ISAAC random number generator diff --git a/rand_pcg/Cargo.toml b/rand_pcg/Cargo.toml index 5ebf2db69d0..93b05da2a37 100644 --- a/rand_pcg/Cargo.toml +++ b/rand_pcg/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_pcg" +documentation = "https://rust-random.github.io/rand/rand_pcg/" homepage = "https://crates.io/crates/rand_pcg" description = """ Selected PCG random number generators diff --git a/rand_xorshift/Cargo.toml b/rand_xorshift/Cargo.toml index e01ddc233f0..81b2d831efc 100644 --- a/rand_xorshift/Cargo.toml +++ b/rand_xorshift/Cargo.toml @@ -5,7 +5,7 @@ authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" repository = "https://github.com/rust-random/rand" -documentation = "https://rust-random.github.io/rand/rand_xorshift" +documentation = "https://rust-random.github.io/rand/rand_xorshift/" homepage = "https://crates.io/crates/rand_xorshift" description = """ Xorshift random number generator From c9fd8897a364e4a1558d18294e6fac55805ddf9c Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 17 Apr 2019 15:29:42 +0100 Subject: [PATCH 02/10] Add explanations of cross-version crate compatibility to README --- README.md | 9 +++++++++ rand_core/README.md | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/README.md b/README.md index 314a57fd60c..d7f74fdc926 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,15 @@ To get started using Rand, see [The Book](https://rust-random.github.io/book). ## Versions +Rand libs have inter-dependencies and make use of the +[semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits +compatible across crate versions. (This is especially important for `RngCore` +and `SeedableRng`.) A few crate releases are thus compatibility shims, +depending on the *next* lib version (e.g. `rand_core` versions `0.2.2` and +`0.3.1`). This means, for example, that `rand_core_0_4_0::SeedableRng` and +`rand_core_0_3_0::SeedableRng` are distinct, incompatible traits, which can +cause build errors. Usually, running `cargo update` is enough to fix any issues. + The Rand lib is not yet stable, however we are careful to limit breaking changes and warn via deprecation wherever possible. Patch versions never introduce breaking changes. The following minor versions are supported: diff --git a/rand_core/README.md b/rand_core/README.md index ef076b99d82..0ac8b919b39 100644 --- a/rand_core/README.md +++ b/rand_core/README.md @@ -40,6 +40,17 @@ The `rand_core` crate provides: The traits and error types are also available via `rand`. +## Versions + +Rand libs have inter-dependencies and make use of the +[semver trick](https://github.com/dtolnay/semver-trick/) in order to make traits +compatible across crate versions. (This is especially important for `RngCore` +and `SeedableRng`.) A few crate releases are thus compatibility shims, +depending on the *next* lib version (e.g. `rand_core` versions `0.2.2` and +`0.3.1`). This means, for example, that `rand_core_0_4_0::SeedableRng` and +`rand_core_0_3_0::SeedableRng` are distinct, incompatible traits, which can +cause build errors. Usually, running `cargo update` is enough to fix any issues. + ## Crate Features `rand_core` supports `no_std` and `alloc`-only configurations, as well as full From 7895ad4f655325141e55a3ec9fae87dab3d9014f Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 19 Apr 2019 14:43:37 +0200 Subject: [PATCH 03/10] fix unaligned and illegal-provenance reads in next_u64 --- rand_core/src/block.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 3045b9482f4..6772a18a895 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -51,7 +51,7 @@ //! [`fill_bytes`]: RngCore::fill_bytes use core::convert::AsRef; -use core::fmt; +use core::{fmt, ptr}; use {RngCore, CryptoRng, SeedableRng, Error}; use impls::{fill_via_u32_chunks, fill_via_u64_chunks}; @@ -183,7 +183,8 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> let read_u64 = |results: &[u32], index| { if cfg!(any(target_arch = "x86", target_arch = "x86_64")) { // requires little-endian CPU supporting unaligned reads: - unsafe { *(&results[index] as *const u32 as *const u64) } + let ptr: *const u64 = results[index..index+1].as_ptr() as *const u64; + unsafe { ptr::read_unaligned(ptr) } } else { let x = u64::from(results[index]); let y = u64::from(results[index + 1]); From 26456b5e1e669c0545034b2e85bcc964168a23fe Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 22 Apr 2019 11:42:07 +0100 Subject: [PATCH 04/10] Remove specialisations of fill_bytes for block RNGs These specialisations relied on casting a u8 byte slice to a u32 or u64 slice, which is UB due to alignment requirements. --- rand_core/src/block.rs | 84 ------------------------------------------ 1 file changed, 84 deletions(-) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 6772a18a895..0d4caae7261 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -210,48 +210,6 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> } } - // As an optimization we try to write directly into the output buffer. - // This is only enabled for little-endian platforms where unaligned writes - // are known to be safe and fast. - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] - fn fill_bytes(&mut self, dest: &mut [u8]) { - let mut filled = 0; - - // Continue filling from the current set of results - if self.index < self.results.as_ref().len() { - let (consumed_u32, filled_u8) = - fill_via_u32_chunks(&self.results.as_ref()[self.index..], - dest); - - self.index += consumed_u32; - filled += filled_u8; - } - - let len_remainder = - (dest.len() - filled) % (self.results.as_ref().len() * 4); - let end_direct = dest.len() - len_remainder; - - while filled < end_direct { - let dest_u32: &mut R::Results = unsafe { - &mut *(dest[filled..].as_mut_ptr() as - *mut ::Results) - }; - self.core.generate(dest_u32); - filled += self.results.as_ref().len() * 4; - self.index = self.results.as_ref().len(); - } - - if len_remainder > 0 { - self.core.generate(&mut self.results); - let (consumed_u32, _) = - fill_via_u32_chunks(self.results.as_ref(), - &mut dest[filled..]); - - self.index = consumed_u32; - } - } - - #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; while read_len < dest.len() { @@ -416,48 +374,6 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> value } - // As an optimization we try to write directly into the output buffer. - // This is only enabled for little-endian platforms where unaligned writes - // are known to be safe and fast. - #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] - fn fill_bytes(&mut self, dest: &mut [u8]) { - let mut filled = 0; - self.half_used = false; - - // Continue filling from the current set of results - if self.index < self.results.as_ref().len() { - let (consumed_u64, filled_u8) = - fill_via_u64_chunks(&self.results.as_ref()[self.index..], - dest); - - self.index += consumed_u64; - filled += filled_u8; - } - - let len_remainder = - (dest.len() - filled) % (self.results.as_ref().len() * 8); - let end_direct = dest.len() - len_remainder; - - while filled < end_direct { - let dest_u64: &mut R::Results = unsafe { - ::core::mem::transmute(dest[filled..].as_mut_ptr()) - }; - self.core.generate(dest_u64); - filled += self.results.as_ref().len() * 8; - self.index = self.results.as_ref().len(); - } - - if len_remainder > 0 { - self.core.generate(&mut self.results); - let (consumed_u64, _) = - fill_via_u64_chunks(&mut self.results.as_ref(), - &mut dest[filled..]); - - self.index = consumed_u64; - } - } - - #[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; self.half_used = false; From 3bede5f59df5f752703c021ab07e406fb3db9f67 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 22 Apr 2019 20:09:42 +0200 Subject: [PATCH 05/10] fix extracting a u64 on little-endian CPUs --- rand_core/src/block.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 0d4caae7261..6c5beca271a 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -181,9 +181,9 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> #[inline(always)] fn next_u64(&mut self) -> u64 { let read_u64 = |results: &[u32], index| { - if cfg!(any(target_arch = "x86", target_arch = "x86_64")) { - // requires little-endian CPU supporting unaligned reads: - let ptr: *const u64 = results[index..index+1].as_ptr() as *const u64; + if cfg!(any(target_endian = "little")) { + // requires little-endian CPU + let ptr: *const u64 = results[index..=index+1].as_ptr() as *const u64; unsafe { ptr::read_unaligned(ptr) } } else { let x = u64::from(results[index]); From eda7ac52d0e991999d8dedd11d35d592c7f9922b Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Thu, 6 Jun 2019 09:32:09 +0100 Subject: [PATCH 06/10] Adjust inlining See #817 --- rand_core/CHANGELOG.md | 1 + rand_core/src/block.rs | 29 +++++++++++++++++++++++------ rand_hc/CHANGELOG.md | 1 + rand_hc/src/hc128.rs | 9 +++++++-- rand_isaac/CHANGELOG.md | 1 + rand_isaac/src/isaac.rs | 9 +++++++-- rand_isaac/src/isaac64.rs | 9 +++++++-- src/rngs/small.rs | 4 ++++ src/rngs/std.rs | 4 ++++ 9 files changed, 55 insertions(+), 12 deletions(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index 7f2d7978fdc..c67f5a941ca 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. 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). +- Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64` ## [0.4.0] - 2019-01-24 - Disable the `std` feature by default (#702) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index 6c5beca271a..e4f07e74bc7 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -131,6 +131,7 @@ impl fmt::Debug for BlockRng { impl BlockRng { /// Create a new `BlockRng` from an existing RNG implementing /// `BlockRngCore`. Results will be generated on first use. + #[inline] pub fn new(core: R) -> BlockRng{ let results_empty = R::Results::default(); BlockRng { @@ -145,18 +146,21 @@ impl BlockRng { /// 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. + #[inline(always)] 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. + #[inline] 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. + #[inline] pub fn generate_and_set(&mut self, index: usize) { assert!(index < self.results.as_ref().len()); self.core.generate(&mut self.results); @@ -167,7 +171,7 @@ impl BlockRng { impl> RngCore for BlockRng where ::Results: AsRef<[u32]> + AsMut<[u32]> { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { if self.index >= self.results.as_ref().len() { self.generate_and_set(0); @@ -178,7 +182,7 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> value } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { let read_u64 = |results: &[u32], index| { if cfg!(any(target_endian = "little")) { @@ -210,6 +214,7 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> } } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; while read_len < dest.len() { @@ -225,23 +230,26 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> } } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { - self.fill_bytes(dest); - Ok(()) + Ok(self.fill_bytes(dest)) } } impl SeedableRng for BlockRng { type Seed = R::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { Self::new(R::from_seed(seed)) } + #[inline(always)] fn seed_from_u64(seed: u64) -> Self { Self::new(R::seed_from_u64(seed)) } + #[inline(always)] fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } @@ -296,6 +304,7 @@ impl fmt::Debug for BlockRng64 { impl BlockRng64 { /// Create a new `BlockRng` from an existing RNG implementing /// `BlockRngCore`. Results will be generated on first use. + #[inline] pub fn new(core: R) -> BlockRng64{ let results_empty = R::Results::default(); BlockRng64 { @@ -311,12 +320,14 @@ impl BlockRng64 { /// 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. + #[inline(always)] 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. + #[inline] pub fn reset(&mut self) { self.index = self.results.as_ref().len(); self.half_used = false; @@ -324,6 +335,7 @@ impl BlockRng64 { /// Generate a new set of results immediately, setting the index to the /// given value. + #[inline] pub fn generate_and_set(&mut self, index: usize) { assert!(index < self.results.as_ref().len()); self.core.generate(&mut self.results); @@ -335,7 +347,7 @@ impl BlockRng64 { impl> RngCore for BlockRng64 where ::Results: AsRef<[u64]> + AsMut<[u64]> { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { let mut index = self.index * 2 - self.half_used as usize; if index >= self.results.as_ref().len() * 2 { @@ -361,7 +373,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> } } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { if self.index >= self.results.as_ref().len() { self.core.generate(&mut self.results); @@ -374,6 +386,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> value } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { let mut read_len = 0; self.half_used = false; @@ -392,6 +405,7 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> } } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { Ok(self.fill_bytes(dest)) } @@ -400,14 +414,17 @@ where ::Results: AsRef<[u64]> + AsMut<[u64]> impl SeedableRng for BlockRng64 { type Seed = R::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { Self::new(R::from_seed(seed)) } + #[inline(always)] fn seed_from_u64(seed: u64) -> Self { Self::new(R::seed_from_u64(seed)) } + #[inline(always)] fn from_rng(rng: S) -> Result { Ok(Self::new(R::from_rng(rng)?)) } diff --git a/rand_hc/CHANGELOG.md b/rand_hc/CHANGELOG.md index d0c4a2fcbc5..3621ee94a96 100644 --- a/rand_hc/CHANGELOG.md +++ b/rand_hc/CHANGELOG.md @@ -4,5 +4,6 @@ All notable changes to this project will be documented in this file. 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). +- Adjust usage of `#[inline]` ## [0.1.0] - 2018-10-17 - Pulled out of the Rand crate diff --git a/rand_hc/src/hc128.rs b/rand_hc/src/hc128.rs index 6c55ec029f4..a320f48f441 100644 --- a/rand_hc/src/hc128.rs +++ b/rand_hc/src/hc128.rs @@ -67,20 +67,22 @@ const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv pub struct Hc128Rng(BlockRng); impl RngCore for Hc128Rng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -89,10 +91,12 @@ impl RngCore for Hc128Rng { impl SeedableRng for Hc128Rng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { Hc128Rng(BlockRng::::from_seed(seed)) } + #[inline] fn from_rng(rng: R) -> Result { BlockRng::::from_rng(rng).map(Hc128Rng) } @@ -268,6 +272,7 @@ impl Hc128Core { // Initialize an HC-128 random number generator. The seed has to be // 256 bits in length (`[u32; 8]`), matching the 128 bit `key` followed by // 128 bit `iv` when HC-128 where to be used as a stream cipher. + #[inline(always)] // single use: SeedableRng::from_seed fn init(seed: [u32; SEED_WORDS]) -> Self { #[inline] fn f1(x: u32) -> u32 { diff --git a/rand_isaac/CHANGELOG.md b/rand_isaac/CHANGELOG.md index fb1ab3f3c57..eb7027f28c0 100644 --- a/rand_isaac/CHANGELOG.md +++ b/rand_isaac/CHANGELOG.md @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file. 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). +- Adjust usage of `#[inline]` ## [0.1.1] - 2018-11-26 - Fix `rand_core` version requirement - Fix doc links diff --git a/rand_isaac/src/isaac.rs b/rand_isaac/src/isaac.rs index 6b596053853..8697577b79c 100644 --- a/rand_isaac/src/isaac.rs +++ b/rand_isaac/src/isaac.rs @@ -92,20 +92,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; pub struct IsaacRng(BlockRng); impl RngCore for IsaacRng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -114,6 +116,7 @@ impl RngCore for IsaacRng { impl SeedableRng for IsaacRng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { IsaacRng(BlockRng::::from_seed(seed)) } @@ -121,10 +124,12 @@ impl SeedableRng for IsaacRng { /// Create an ISAAC random number generator using an `u64` as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded. + #[inline] fn seed_from_u64(seed: u64) -> Self { IsaacRng(BlockRng::::seed_from_u64(seed)) } + #[inline] fn from_rng(rng: S) -> Result { BlockRng::::from_rng(rng).map(|rng| IsaacRng(rng)) } diff --git a/rand_isaac/src/isaac64.rs b/rand_isaac/src/isaac64.rs index b5db351d2ea..5450a51d882 100644 --- a/rand_isaac/src/isaac64.rs +++ b/rand_isaac/src/isaac64.rs @@ -83,20 +83,22 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; pub struct Isaac64Rng(BlockRng64); impl RngCore for Isaac64Rng { - #[inline(always)] + #[inline] fn next_u32(&mut self) -> u32 { self.0.next_u32() } - #[inline(always)] + #[inline] fn next_u64(&mut self) -> u64 { self.0.next_u64() } + #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest) } + #[inline] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -105,6 +107,7 @@ impl RngCore for Isaac64Rng { impl SeedableRng for Isaac64Rng { type Seed = ::Seed; + #[inline] fn from_seed(seed: Self::Seed) -> Self { Isaac64Rng(BlockRng64::::from_seed(seed)) } @@ -112,10 +115,12 @@ impl SeedableRng for Isaac64Rng { /// Create an ISAAC random number generator using an `u64` as seed. /// If `seed == 0` this will produce the same stream of random numbers as /// the reference implementation when used unseeded. + #[inline] fn seed_from_u64(seed: u64) -> Self { Isaac64Rng(BlockRng64::::seed_from_u64(seed)) } + #[inline] fn from_rng(rng: S) -> Result { BlockRng64::::from_rng(rng).map(|rng| Isaac64Rng(rng)) } diff --git a/src/rngs/small.rs b/src/rngs/small.rs index f27bdacfa97..64a9d933115 100644 --- a/src/rngs/small.rs +++ b/src/rngs/small.rs @@ -81,10 +81,12 @@ impl RngCore for SmallRng { self.0.next_u64() } + #[inline(always)] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest); } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -93,10 +95,12 @@ impl RngCore for SmallRng { impl SeedableRng for SmallRng { type Seed = ::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { SmallRng(Rng::from_seed(seed)) } + #[inline(always)] fn from_rng(rng: R) -> Result { Rng::from_rng(rng).map(SmallRng) } diff --git a/src/rngs/std.rs b/src/rngs/std.rs index ae6d327c9fb..3d753c66baa 100644 --- a/src/rngs/std.rs +++ b/src/rngs/std.rs @@ -39,10 +39,12 @@ impl RngCore for StdRng { self.0.next_u64() } + #[inline(always)] fn fill_bytes(&mut self, dest: &mut [u8]) { self.0.fill_bytes(dest); } + #[inline(always)] fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { self.0.try_fill_bytes(dest) } @@ -51,10 +53,12 @@ impl RngCore for StdRng { impl SeedableRng for StdRng { type Seed = ::Seed; + #[inline(always)] fn from_seed(seed: Self::Seed) -> Self { StdRng(Hc128Rng::from_seed(seed)) } + #[inline(always)] fn from_rng(rng: R) -> Result { Hc128Rng::from_rng(rng).map(StdRng) } From 668810056be5607cf040839d36cd14f45c672f8b Mon Sep 17 00:00:00 2001 From: Virgile Andreani Date: Tue, 23 Jul 2019 18:59:02 +0200 Subject: [PATCH 07/10] Fix syntax of some links in documentation --- rand_core/src/lib.rs | 10 +++++----- src/distributions/exponential.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/rand_core/src/lib.rs b/rand_core/src/lib.rs index 4b0e6e48b01..28e7ac11d72 100644 --- a/rand_core/src/lib.rs +++ b/rand_core/src/lib.rs @@ -141,23 +141,23 @@ pub trait RngCore { /// RNGs must implement at least one method from this trait directly. In /// the case this method is not implemented directly, it can be implemented /// using `self.next_u64() as u32` or via - /// [`fill_bytes`][impls::next_u32_via_fill]. + /// [`fill_bytes`](impls::next_u32_via_fill). fn next_u32(&mut self) -> u32; /// Return the next random `u64`. /// /// RNGs must implement at least one method from this trait directly. In /// the case this method is not implemented directly, it can be implemented - /// via [`next_u32`][impls::next_u64_via_u32] or via - /// [`fill_bytes`][impls::next_u64_via_fill]. + /// via [`next_u32`](impls::next_u64_via_u32) or via + /// [`fill_bytes`](impls::next_u64_via_fill). fn next_u64(&mut self) -> u64; /// Fill `dest` with random data. /// /// RNGs must implement at least one method from this trait directly. In /// the case this method is not implemented directly, it can be implemented - /// via [`next_u*`][impls::fill_bytes_via_next] or - /// via [`try_fill_bytes`][RngCore::try_fill_bytes]; if this generator can + /// via [`next_u*`](impls::fill_bytes_via_next) or + /// via [`try_fill_bytes`](RngCore::try_fill_bytes); if this generator can /// fail the implementation must choose how best to handle errors here /// (e.g. panic with a descriptive message or log a warning and retry a few /// times). diff --git a/src/distributions/exponential.rs b/src/distributions/exponential.rs index 76752a60e3d..e6b61d7a793 100644 --- a/src/distributions/exponential.rs +++ b/src/distributions/exponential.rs @@ -64,7 +64,7 @@ impl Distribution for Exp1 { /// This distribution has density function: `f(x) = lambda * exp(-lambda * x)` /// for `x > 0`. /// -/// Note that [`Exp1`][crate::distributions::Exp1] is an optimised implementation for `lambda = 1`. +/// Note that [`Exp1`](crate::distributions::Exp1) is an optimised implementation for `lambda = 1`. /// /// # Example /// From 56c171fe163fe832fc498e79af3cf05e2d831851 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sat, 27 Jul 2019 11:24:46 +0100 Subject: [PATCH 08/10] Workaround to support older Rustc --- rand_core/src/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rand_core/src/block.rs b/rand_core/src/block.rs index e4f07e74bc7..7d912639bb1 100644 --- a/rand_core/src/block.rs +++ b/rand_core/src/block.rs @@ -187,7 +187,7 @@ where ::Results: AsRef<[u32]> + AsMut<[u32]> let read_u64 = |results: &[u32], index| { if cfg!(any(target_endian = "little")) { // requires little-endian CPU - let ptr: *const u64 = results[index..=index+1].as_ptr() as *const u64; + let ptr: *const u64 = results[index..index+2].as_ptr() as *const u64; unsafe { ptr::read_unaligned(ptr) } } else { let x = u64::from(results[index]); From b1eea36e3e353f15bccb298ac7feb80223541db9 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Sat, 27 Jul 2019 11:25:03 +0100 Subject: [PATCH 09/10] Prepare rand_core 0.4.1 release --- rand_core/CHANGELOG.md | 4 ++++ rand_core/Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index c67f5a941ca..41f072e5e88 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -4,7 +4,11 @@ All notable changes to this project will be documented in this file. 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.4.1] - 2019-07-29 +Back-port non-API changes from version 0.5.0: +- Enable testing with Miri and fix incorrect pointer usages (#779, #780, #781, #783, #784) - Adjust usage of `#[inline]` for `BlockRng` and `BlockRng64` + ## [0.4.0] - 2019-01-24 - Disable the `std` feature by default (#702) diff --git a/rand_core/Cargo.toml b/rand_core/Cargo.toml index 98e47ddf899..3be2f9b3676 100644 --- a/rand_core/Cargo.toml +++ b/rand_core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rand_core" -version = "0.4.0" +version = "0.4.1" authors = ["The Rand Project Developers", "The Rust Project Developers"] license = "MIT/Apache-2.0" readme = "README.md" From 1b58378f0867a23bc825e176abfa22b7fda89da3 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Wed, 31 Jul 2019 14:36:04 +0100 Subject: [PATCH 10/10] Disable serde1 feature in tests of rand This feature is not applicable to rand, but is causing issues due to a bug in average. --- .travis.yml | 11 ++++++----- appveyor.yml | 2 +- utils/ci/script.sh | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2360b235c53..a0593cdd7a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -40,7 +40,7 @@ sudo: false # - test no_std support, but only the unit tests: # `cargo test --lib --no-default-features` # - run unit tests and doctests with all features which are available on stable: -# `cargo test --features=serde1,log` +# `cargo test --features=log` # - test examples: # `cargo test --examples` # Additional tests on nightly: @@ -64,7 +64,7 @@ matrix: # Differs from standard script: rand_pcg features - cargo test --lib --no-default-features # TODO: add simd_support feature: - - cargo test --features=serde1,log + - cargo test --features=log - cargo test --examples - cargo test --manifest-path rand_core/Cargo.toml - cargo test --manifest-path rand_core/Cargo.toml --no-default-features @@ -86,7 +86,7 @@ matrix: # Differs from standard script: includes aarch64-apple-ios cross-build - cargo test --lib --no-default-features # TODO: add simd_support feature: - - cargo test --features=serde1,log + - cargo test --features=log - cargo test --examples - cargo test --manifest-path rand_core/Cargo.toml - cargo test --manifest-path rand_core/Cargo.toml --no-default-features @@ -113,7 +113,8 @@ matrix: script: # Differs from standard script: alloc feature, all features, doc build - cargo test --lib --no-default-features --features=alloc - - cargo test --all-features + # Excludes serde1: + - cargo test --features=nightly,simd_support,log - cargo test --benches --features=nightly - cargo test --examples - cargo test --manifest-path rand_core/Cargo.toml @@ -207,7 +208,7 @@ before_install: script: - cargo test --lib --no-default-features # TODO: add simd_support feature: - - cargo test --features=serde1,log + - cargo test --features=log - cargo test --examples - cargo test --manifest-path rand_core/Cargo.toml - cargo test --manifest-path rand_core/Cargo.toml --no-default-features diff --git a/appveyor.yml b/appveyor.yml index 70a8cada336..c02adc8b9ab 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -34,7 +34,7 @@ build: false test_script: - cargo test --lib --no-default-features --features alloc # TODO: use --all-features once simd_support is sufficiently stable: - - cargo test --features=serde1,log + - cargo test --features=log - cargo test --benches --features=nightly - cargo test --examples - cargo test --manifest-path rand_core/Cargo.toml diff --git a/utils/ci/script.sh b/utils/ci/script.sh index d6338cce2fe..cf52a6e9205 100644 --- a/utils/ci/script.sh +++ b/utils/ci/script.sh @@ -5,7 +5,7 @@ set -ex main() { cross test --target $TARGET --lib --no-default-features # TODO: add simd_support feature: - cross test --target $TARGET --features=serde1,log + cross test --target $TARGET --features=log cross test --target $TARGET --examples cross test --target $TARGET --manifest-path rand_core/Cargo.toml cross test --target $TARGET --manifest-path rand_core/Cargo.toml --no-default-features