Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix next_u64_via_fill; rand_core changelog #1061

Merged
merged 3 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions rand_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ 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 (#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)

## [0.5.1] - 2019-08-28
- `OsRng` added to `rand_core` (#863)
- `Error::INTERNAL_START` and `Error::CUSTOM_START` constants (#864)
Expand Down
3 changes: 3 additions & 0 deletions rand_core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions rand_core/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ pub fn fill_via_u64_chunks(src: &[u64], dest: &mut [u8]) -> (usize, usize) {
pub fn next_u32_via_fill<R: RngCore + ?Sized>(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<R: RngCore + ?Sized>(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)]
Expand Down
18 changes: 9 additions & 9 deletions src/rngs/adapter/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down