From 88b50e69941e48415f233e6eb3f2b7944ff0fc87 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 19 Oct 2020 10:32:21 +0100 Subject: [PATCH 1/3] Fix next_u32_via_fill and next_u64_via_fill to use LE conversion as documented --- rand_core/src/impls.rs | 4 ++-- src/rngs/adapter/read.rs | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/rand_core/src/impls.rs b/rand_core/src/impls.rs index 79258364618..2588a72ea3f 100644 --- a/rand_core/src/impls.rs +++ b/rand_core/src/impls.rs @@ -136,14 +136,14 @@ pub fn fill_via_u64_chunks(src: &[u64], dest: &mut [u8]) -> (usize, usize) { pub fn next_u32_via_fill(rng: &mut R) -> u32 { let mut buf = [0; 4]; rng.fill_bytes(&mut buf); - u32::from_ne_bytes(buf) + u32::from_le_bytes(buf) } /// Implement `next_u64` via `fill_bytes`, little-endian order. pub fn next_u64_via_fill(rng: &mut R) -> u64 { let mut buf = [0; 8]; rng.fill_bytes(&mut buf); - u64::from_ne_bytes(buf) + u64::from_le_bytes(buf) } #[cfg(test)] diff --git a/src/rngs/adapter/read.rs b/src/rngs/adapter/read.rs index a5b31cc1bdd..63b0dd0c0f0 100644 --- a/src/rngs/adapter/read.rs +++ b/src/rngs/adapter/read.rs @@ -113,23 +113,23 @@ mod test { // transmute from the target to avoid endianness concerns. #[rustfmt::skip] let v = [0u8, 0, 0, 0, 0, 0, 0, 1, - 0, 0, 0, 0, 0, 0, 0, 2, - 0, 0, 0, 0, 0, 0, 0, 3]; + 0, 4, 0, 0, 3, 0, 0, 2, + 5, 0, 0, 0, 0, 0, 0, 0]; let mut rng = ReadRng::new(&v[..]); - assert_eq!(rng.next_u64(), 1_u64.to_be()); - assert_eq!(rng.next_u64(), 2_u64.to_be()); - assert_eq!(rng.next_u64(), 3_u64.to_be()); + assert_eq!(rng.next_u64(), 1 << 56); + assert_eq!(rng.next_u64(), (2 << 56) + (3 << 32) + (4 << 8)); + assert_eq!(rng.next_u64(), 5); } #[test] fn test_reader_rng_u32() { - let v = [0u8, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3]; + let v = [0u8, 0, 0, 1, 0, 0, 2, 0, 3, 0, 0, 0]; let mut rng = ReadRng::new(&v[..]); - assert_eq!(rng.next_u32(), 1_u32.to_be()); - assert_eq!(rng.next_u32(), 2_u32.to_be()); - assert_eq!(rng.next_u32(), 3_u32.to_be()); + assert_eq!(rng.next_u32(), 1 << 24); + assert_eq!(rng.next_u32(), 2 << 16); + assert_eq!(rng.next_u32(), 3); } #[test] From c9f118ecd5daf030367ff7acd36923cf8864f881 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 19 Oct 2020 10:32:51 +0100 Subject: [PATCH 2/3] Update changelog(s), esp. for rand_core v0.6 --- CHANGELOG.md | 2 ++ rand_core/CHANGELOG.md | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d6cc136d75..61f945cabf3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update. - The `nightly` feature no longer implies the `simd_support` feature (#1048) - Fix `simd_support` feature to work on current nightlies (#1056) - Improve accuracy and performance of `IteratorRandom::choose` (#1059) +- `ReadRng::next_u32` and `next_u64` now use little-Endian conversion instead + of native-Endian, affecting results on Big-Endian platforms (#1026) ## [0.7.3] - 2020-01-10 ### Fixes diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index dfdd6928ef0..9f6f91c4955 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -4,6 +4,17 @@ 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). +## [Unreleased] + +- Bump MSRV to 1.36, various code improvements (#1011) +- Update to getrandom v0.2 (#1041) +- Fix: `next_u32_via_fill` and `next_u64_via_fill` now use LE as documented (#?) +- Reduce usage of `unsafe` (#962, #963, #1011) +- Annotate feature-gates in documentation (#1019) +- Various documentation tweaks +- Fix some clippy warnings (#1036) +- Apply rustfmt (#926) + ## [0.5.1] - 2019-08-28 - `OsRng` added to `rand_core` (#863) - `Error::INTERNAL_START` and `Error::CUSTOM_START` constants (#864) From 60e0339387bd15f066e6217598a6b8aeff21ea27 Mon Sep 17 00:00:00 2001 From: Diggory Hardy Date: Mon, 19 Oct 2020 11:10:29 +0100 Subject: [PATCH 3/3] Document available error codes --- rand_core/CHANGELOG.md | 3 ++- rand_core/src/error.rs | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/rand_core/CHANGELOG.md b/rand_core/CHANGELOG.md index 9f6f91c4955..a33247ddafe 100644 --- a/rand_core/CHANGELOG.md +++ b/rand_core/CHANGELOG.md @@ -8,9 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump MSRV to 1.36, various code improvements (#1011) - Update to getrandom v0.2 (#1041) -- Fix: `next_u32_via_fill` and `next_u64_via_fill` now use LE as documented (#?) +- Fix: `next_u32_via_fill` and `next_u64_via_fill` now use LE as documented (#1061) - Reduce usage of `unsafe` (#962, #963, #1011) - Annotate feature-gates in documentation (#1019) +- Document available error codes (#1061) - Various documentation tweaks - Fix some clippy warnings (#1036) - Apply rustfmt (#926) diff --git a/rand_core/src/error.rs b/rand_core/src/error.rs index d2e467da84d..a64c430da8b 100644 --- a/rand_core/src/error.rs +++ b/rand_core/src/error.rs @@ -29,6 +29,9 @@ impl Error { /// Codes at or above this point can be used by users to define their own /// custom errors. /// + /// This has a fixed value of `(1 << 31) + (1 << 30) = 0xC000_0000`, + /// therefore the number of values available for custom codes is `1 << 30`. + /// /// This is identical to [`getrandom::Error::CUSTOM_START`](https://docs.rs/getrandom/latest/getrandom/struct.Error.html#associatedconstant.CUSTOM_START). pub const CUSTOM_START: u32 = (1 << 31) + (1 << 30); /// Codes below this point represent OS Errors (i.e. positive i32 values).