diff options
Diffstat (limited to 'rand/rand_xorshift')
-rw-r--r-- | rand/rand_xorshift/CHANGELOG.md | 8 | ||||
-rw-r--r-- | rand/rand_xorshift/Cargo.toml | 14 | ||||
-rw-r--r-- | rand/rand_xorshift/README.md | 4 | ||||
-rw-r--r-- | rand/rand_xorshift/src/lib.rs | 30 | ||||
-rw-r--r-- | rand/rand_xorshift/tests/mod.rs | 11 |
5 files changed, 33 insertions, 34 deletions
diff --git a/rand/rand_xorshift/CHANGELOG.md b/rand/rand_xorshift/CHANGELOG.md index 539af41..ce3098a 100644 --- a/rand/rand_xorshift/CHANGELOG.md +++ b/rand/rand_xorshift/CHANGELOG.md @@ -4,6 +4,14 @@ 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.2.0] - 2019-06-12 +- Bump minor crate version since rand_core bump is a breaking change +- Switch to Edition 2018 + +## [0.1.2] - 2019-06-06 - yanked +- Bump `rand_core` version +- Make XorShiftRng::from_rng portable by enforcing Endianness (#815) + ## [0.1.1] - 2019-01-04 - Reorganise code and tests; tweak doc diff --git a/rand/rand_xorshift/Cargo.toml b/rand/rand_xorshift/Cargo.toml index 114fee9..9cb257b 100644 --- a/rand/rand_xorshift/Cargo.toml +++ b/rand/rand_xorshift/Cargo.toml @@ -1,29 +1,29 @@ [package] name = "rand_xorshift" -version = "0.1.1" +version = "0.2.0" authors = ["The Rand Project Developers", "The Rust Project Developers"] -license = "MIT/Apache-2.0" +license = "MIT OR 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 """ keywords = ["random", "rng", "xorshift"] categories = ["algorithms", "no-std"] +edition = "2018" [badges] travis-ci = { repository = "rust-random/rand" } appveyor = { repository = "rust-random/rand" } [features] -serde1 = ["serde", "serde_derive"] +serde1 = ["serde"] [dependencies] -rand_core = { path = "../rand_core", version = ">=0.2, <0.4", default-features=false } -serde = { version = "1", optional = true } -serde_derive = { version = "^1.0.38", optional = true } +rand_core = { path = "../rand_core", version = "0.5" } +serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] # This is for testing serde, unfortunately we can't specify feature-gated dev diff --git a/rand/rand_xorshift/README.md b/rand/rand_xorshift/README.md index 573ee12..57de284 100644 --- a/rand/rand_xorshift/README.md +++ b/rand/rand_xorshift/README.md @@ -6,7 +6,7 @@ [![Book](https://img.shields.io/badge/book-master-yellow.svg)](https://rust-random.github.io/book/) [![API](https://img.shields.io/badge/api-master-yellow.svg)](https://rust-random.github.io/rand/rand_xorshift) [![API](https://docs.rs/rand_xorshift/badge.svg)](https://docs.rs/rand_xorshift) -[![Minimum rustc version](https://img.shields.io/badge/rustc-1.22+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements) +[![Minimum rustc version](https://img.shields.io/badge/rustc-1.32+-lightgray.svg)](https://github.com/rust-random/rand#rust-version-requirements) Implements the Xorshift random number generator. @@ -22,7 +22,7 @@ Links: - [API documentation (master)](https://rust-random.github.io/rand/rand_xorshift) - [API documentation (docs.rs)](https://docs.rs/rand_xorshift) -- [Changelog](CHANGELOG.md) +- [Changelog](https://github.com/rust-random/rand/blob/master/rand_xorshift/CHANGELOG.md) [rand]: https://crates.io/crates/rand diff --git a/rand/rand_xorshift/src/lib.rs b/rand/rand_xorshift/src/lib.rs index db42ba2..b9fef23 100644 --- a/rand/rand_xorshift/src/lib.rs +++ b/rand/rand_xorshift/src/lib.rs @@ -17,14 +17,10 @@ #![no_std] -pub extern crate rand_core; - -#[cfg(feature="serde1")] extern crate serde; -#[cfg(feature="serde1")] #[macro_use] extern crate serde_derive; - use core::num::Wrapping as w; -use core::{fmt, slice}; +use core::fmt; use rand_core::{RngCore, SeedableRng, Error, impls, le}; +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; /// An Xorshift random number generator. /// @@ -75,7 +71,8 @@ impl RngCore for XorShiftRng { } fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> { - Ok(self.fill_bytes(dest)) + self.fill_bytes(dest); + Ok(()) } } @@ -102,22 +99,19 @@ impl SeedableRng for XorShiftRng { } fn from_rng<R: RngCore>(mut rng: R) -> Result<Self, Error> { - let mut seed_u32 = [0u32; 4]; + let mut b = [0u8; 16]; loop { - unsafe { - let ptr = seed_u32.as_mut_ptr() as *mut u8; - - let slice = slice::from_raw_parts_mut(ptr, 4 * 4); - rng.try_fill_bytes(slice)?; + rng.try_fill_bytes(&mut b[..])?; + if !b.iter().all(|&x| x == 0) { + break; } - if !seed_u32.iter().all(|&x| x == 0) { break; } } Ok(XorShiftRng { - x: w(seed_u32[0]), - y: w(seed_u32[1]), - z: w(seed_u32[2]), - w: w(seed_u32[3]), + x: w(u32::from_le_bytes([b[0], b[1], b[2], b[3]])), + y: w(u32::from_le_bytes([b[4], b[5], b[6], b[7]])), + z: w(u32::from_le_bytes([b[8], b[9], b[10], b[11]])), + w: w(u32::from_le_bytes([b[12], b[13], b[14], b[15]])), }) } } diff --git a/rand/rand_xorshift/tests/mod.rs b/rand/rand_xorshift/tests/mod.rs index 8374b64..7ecdeae 100644 --- a/rand/rand_xorshift/tests/mod.rs +++ b/rand/rand_xorshift/tests/mod.rs @@ -1,7 +1,3 @@ -extern crate rand_core; -extern crate rand_xorshift; -#[cfg(all(feature="serde1", test))] extern crate bincode; - use rand_core::{RngCore, SeedableRng}; use rand_xorshift::XorShiftRng; @@ -12,9 +8,10 @@ fn test_xorshift_construction() { let mut rng1 = XorShiftRng::from_seed(seed); assert_eq!(rng1.next_u64(), 4325440999699518727); - let _rng2 = XorShiftRng::from_rng(rng1).unwrap(); - // Note: we cannot test the state of _rng2 because from_rng does not - // fix Endianness. This is allowed in the trait specification. + let mut rng2 = XorShiftRng::from_rng(&mut rng1).unwrap(); + // Yes, this makes rng2 a clone of rng1! + assert_eq!(rng1.next_u64(), 15614385950550801700); + assert_eq!(rng2.next_u64(), 15614385950550801700); } #[test] |