diff options
Diffstat (limited to 'rand/rand_xoshiro')
-rw-r--r-- | rand/rand_xoshiro/CHANGELOG.md | 16 | ||||
-rw-r--r-- | rand/rand_xoshiro/Cargo.toml | 16 | ||||
-rw-r--r-- | rand/rand_xoshiro/README.md | 10 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/common.rs | 2 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/lib.rs | 18 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/splitmix64.rs | 7 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro128plus.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro128starstar.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro64star.rs | 13 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoroshiro64starstar.rs | 17 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro128plus.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro128starstar.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro256plus.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro256starstar.rs | 8 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro512plus.rs | 10 | ||||
-rw-r--r-- | rand/rand_xoshiro/src/xoshiro512starstar.rs | 10 | ||||
-rw-r--r-- | rand/rand_xoshiro/tests/serde.rs | 83 |
17 files changed, 171 insertions, 79 deletions
diff --git a/rand/rand_xoshiro/CHANGELOG.md b/rand/rand_xoshiro/CHANGELOG.md index b23c990..56cb9c2 100644 --- a/rand/rand_xoshiro/CHANGELOG.md +++ b/rand/rand_xoshiro/CHANGELOG.md @@ -4,5 +4,21 @@ 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.3.1] - 2019-08-06 +- Drop `byteorder`-dependency in favor of `stdlib`-implementation. + +## [0.3.0] - 2019-06-12 +- Bump minor crate version since rand_core bump is a breaking change +- Switch to Edition 2018 + +## [0.2.1] - 2019-06-06 - yanked +- Bump `rand_core` version +- Document crate features in README + +## [0.2.0] - 2019-05-28 +- Fix `seed_from_u64(0)` for `Xoroshiro64StarStar` and `Xoroshiro64Star`. This + breaks value stability for these generators if initialized with `seed_from_u64`. +- Implement Serde support. + ## [0.1.0] - 2019-01-04 Initial release. diff --git a/rand/rand_xoshiro/Cargo.toml b/rand/rand_xoshiro/Cargo.toml index 8d174c7..128c213 100644 --- a/rand/rand_xoshiro/Cargo.toml +++ b/rand/rand_xoshiro/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "rand_xoshiro" -version = "0.1.0" # NB: When modifying, also modify html_root_url in lib.rs +version = "0.3.1" # NB: When modifying, also modify html_root_url in lib.rs authors = ["The Rand 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://docs.rs/rand_xoshiro" @@ -10,10 +10,16 @@ homepage = "https://crates.io/crates/rand_xoshiro" description = "Xoshiro, xoroshiro and splitmix64 random number generators" keywords = ["random", "rng"] categories = ["algorithms"] +edition = "2018" + +[features] +serde1 = ["serde"] [dependencies] -byteorder = { version = "1", default-features=false } -rand_core = { path = "../rand_core", version = "0.3", default-features=false } +rand_core = { path = "../rand_core", version = "0.5" } +serde = { version = "1", features = ["derive"], optional=true } [dev-dependencies] -rand = { path = "..", version = "0.6", default-features=false } # needed for doctests +# This is for testing serde, unfortunately we can't specify feature-gated dev +# deps yet, see: https://github.com/rust-lang/cargo/issues/1596 +bincode = { version = "1" } diff --git a/rand/rand_xoshiro/README.md b/rand/rand_xoshiro/README.md index 014477e..1c02992 100644 --- a/rand/rand_xoshiro/README.md +++ b/rand/rand_xoshiro/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_xoshiro) [![API](https://docs.rs/rand_xoshiro/badge.svg)](https://docs.rs/rand_xoshiro) -[![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) Rust implementation of the [xoshiro, xoroshiro and splitmix64](http://xoshiro.di.unimi.it) random number generators. @@ -17,7 +17,13 @@ Links: - [API documentation (master)](https://rust-random.github.io/rand/rand_xoshiro) - [API documentation (docs.rs)](https://docs.rs/rand_xoshiro) -- [Changelog](CHANGELOG.md) +- [Changelog](https://github.com/rust-random/rand/blob/master/rand_xoshiro/CHANGELOG.md) + +## Crate Features + +`rand_xoshiro` is no_std compatible by default. + +The `serde1` feature includes implementations of `Serialize` and `Deserialize` for the included RNGs. ## License diff --git a/rand/rand_xoshiro/src/common.rs b/rand/rand_xoshiro/src/common.rs index 9ee09e2..b188dd6 100644 --- a/rand/rand_xoshiro/src/common.rs +++ b/rand/rand_xoshiro/src/common.rs @@ -9,7 +9,7 @@ /// Initialize a RNG from a `u64` seed using `SplitMix64`. macro_rules! from_splitmix { ($seed:expr) => { { - let mut rng = ::SplitMix64::seed_from_u64($seed); + let mut rng = crate::SplitMix64::seed_from_u64($seed); Self::from_rng(&mut rng).unwrap() } } } diff --git a/rand/rand_xoshiro/src/lib.rs b/rand/rand_xoshiro/src/lib.rs index 634db31..3047e92 100644 --- a/rand/rand_xoshiro/src/lib.rs +++ b/rand/rand_xoshiro/src/lib.rs @@ -55,28 +55,15 @@ //! //! [xoshiro]: http://xoshiro.di.unimi.it/ //! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php -//! [`Xoshiro256StarStar`]: ./struct.Xoshiro256StarStar.html -//! [`Xoshiro256Plus`]: ./struct.Xoshiro256Plus.html -//! [`Xoroshiro128StarStar`]: ./struct.Xoroshiro128StarStar.html -//! [`Xoroshiro128Plus`]: ./struct.Xoroshiro128Plus.html -//! [`Xoshiro512StarStar`]: ./struct.Xoshiro512StarStar.html -//! [`Xoshiro512Plus`]: ./struct.Xoshiro512Plus.html -//! [`SplitMix64`]: ./struct.SplitMix64.html -//! [`Xoshiro128StarStar`]: ./struct.Xoshiro128StarStar.html -//! [`Xoshiro128Plus`]: ./struct.Xoshiro128Plus.html -//! [`Xoroshiro64StarStar`]: ./struct.Xoroshiro64StarStar.html -//! [`Xoroshiro64Star`]: ./struct.Xoroshiro64Star.html #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk.png", html_favicon_url = "https://www.rust-lang.org/favicon.ico", - html_root_url = "https://docs.rs/rand_xoshiro/0.1.0")] + html_root_url = "https://docs.rs/rand_xoshiro/0.3.1")] #![deny(missing_docs)] #![deny(missing_debug_implementations)] -#![cfg_attr(feature = "cargo-clippy", allow(unreadable_literal))] +#![allow(clippy::unreadable_literal)] #![no_std] -extern crate byteorder; -pub extern crate rand_core; #[macro_use] mod common; @@ -92,6 +79,7 @@ mod xoroshiro128starstar; mod xoroshiro64starstar; mod xoroshiro64star; +pub use rand_core; pub use splitmix64::SplitMix64; pub use xoshiro128starstar::Xoshiro128StarStar; pub use xoshiro128plus::Xoshiro128Plus; diff --git a/rand/rand_xoshiro/src/splitmix64.rs b/rand/rand_xoshiro/src/splitmix64.rs index a7cac9f..3a41450 100644 --- a/rand/rand_xoshiro/src/splitmix64.rs +++ b/rand/rand_xoshiro/src/splitmix64.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use byteorder::{ByteOrder, LittleEndian}; +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; use rand_core::{RngCore, SeedableRng, Error}; @@ -22,6 +22,7 @@ use rand_core::{RngCore, SeedableRng, Error}; /// from [`dsiutils`](http://dsiutils.di.unimi.it/) is used. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct SplitMix64 { x: u64, } @@ -77,9 +78,7 @@ impl SeedableRng for SplitMix64 { /// Seed a `SplitMix64` from a `u64`. fn seed_from_u64(seed: u64) -> SplitMix64 { - let mut x = [0; 8]; - LittleEndian::write_u64(&mut x, seed); - SplitMix64::from_seed(x) + SplitMix64::from_seed(seed.to_le_bytes()) } } diff --git a/rand/rand_xoshiro/src/xoroshiro128plus.rs b/rand/rand_xoshiro/src/xoroshiro128plus.rs index df032c8..a7b4ebc 100644 --- a/rand/rand_xoshiro/src/xoroshiro128plus.rs +++ b/rand/rand_xoshiro/src/xoroshiro128plus.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core; use rand_core::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; @@ -22,6 +23,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128Plus { s0: u64, s1: u64, @@ -34,10 +36,7 @@ impl Xoroshiro128Plus { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128Plus; /// /// let rng1 = Xoroshiro128Plus::seed_from_u64(0); @@ -45,7 +44,6 @@ impl Xoroshiro128Plus { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]); diff --git a/rand/rand_xoshiro/src/xoroshiro128starstar.rs b/rand/rand_xoshiro/src/xoroshiro128starstar.rs index 2d27850..21823f5 100644 --- a/rand/rand_xoshiro/src/xoroshiro128starstar.rs +++ b/rand/rand_xoshiro/src/xoroshiro128starstar.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core; use rand_core::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; @@ -21,6 +22,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128StarStar { s0: u64, s1: u64, @@ -33,10 +35,7 @@ impl Xoroshiro128StarStar { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128StarStar; /// /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0); @@ -44,7 +43,6 @@ impl Xoroshiro128StarStar { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [0xdf900294d8f554a5, 0x170865df4b3201fc]); diff --git a/rand/rand_xoshiro/src/xoroshiro64star.rs b/rand/rand_xoshiro/src/xoroshiro64star.rs index 86338fd..6bb708a 100644 --- a/rand/rand_xoshiro/src/xoroshiro64star.rs +++ b/rand/rand_xoshiro/src/xoroshiro64star.rs @@ -6,7 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use byteorder::{ByteOrder, LittleEndian}; +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core; use rand_core::le::read_u32_into; use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; @@ -23,6 +23,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro64Star { s0: u32, s1: u32, @@ -71,9 +72,7 @@ impl SeedableRng for Xoroshiro64Star { /// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoroshiro64Star { - let mut s = [0; 8]; - LittleEndian::write_u64(&mut s, seed); - Xoroshiro64Star::from_seed(s) + from_splitmix!(seed) } } @@ -94,4 +93,10 @@ mod tests { assert_eq!(rng.next_u32(), e); } } + + #[test] + fn zero_seed() { + let mut rng = Xoroshiro64Star::seed_from_u64(0); + assert_ne!(rng.next_u64(), 0); + } } diff --git a/rand/rand_xoshiro/src/xoroshiro64starstar.rs b/rand/rand_xoshiro/src/xoroshiro64starstar.rs index a40baee..8e1aea1 100644 --- a/rand/rand_xoshiro/src/xoroshiro64starstar.rs +++ b/rand/rand_xoshiro/src/xoroshiro64starstar.rs @@ -6,13 +6,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use byteorder::{ByteOrder, LittleEndian}; +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core; use rand_core::le::read_u32_into; use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; use rand_core::{RngCore, SeedableRng}; -/// A Xoroshiro64** random number generator. +/// A xoroshiro64** random number generator. /// /// The xoshiro64** algorithm is not suitable for cryptographic purposes, but /// is very fast and has excellent statistical properties. @@ -22,6 +22,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro64StarStar { s0: u32, s1: u32, @@ -68,11 +69,9 @@ impl SeedableRng for Xoroshiro64StarStar { } } - /// Seed a `Xoroshiro64StarStar` from a `u64`. + /// Seed a `Xoroshiro64StarStar` from a `u64` using `SplitMix64`. fn seed_from_u64(seed: u64) -> Xoroshiro64StarStar { - let mut s = [0; 8]; - LittleEndian::write_u64(&mut s, seed); - Xoroshiro64StarStar::from_seed(s) + from_splitmix!(seed) } } @@ -93,4 +92,10 @@ mod tests { assert_eq!(rng.next_u32(), e); } } + + #[test] + fn zero_seed() { + let mut rng = Xoroshiro64StarStar::seed_from_u64(0); + assert_ne!(rng.next_u64(), 0); + } } diff --git a/rand/rand_xoshiro/src/xoshiro128plus.rs b/rand/rand_xoshiro/src/xoshiro128plus.rs index b0c7cc7..7cbd612 100644 --- a/rand/rand_xoshiro/src/xoshiro128plus.rs +++ b/rand/rand_xoshiro/src/xoshiro128plus.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next}; use rand_core::le::read_u32_into; use rand_core::{SeedableRng, RngCore, Error}; @@ -20,6 +21,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128Plus { s: [u32; 4], } @@ -31,10 +33,7 @@ impl Xoshiro128Plus { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128StarStar; /// /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0); @@ -42,7 +41,6 @@ impl Xoshiro128Plus { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]); diff --git a/rand/rand_xoshiro/src/xoshiro128starstar.rs b/rand/rand_xoshiro/src/xoshiro128starstar.rs index 836864e..7af1e50 100644 --- a/rand/rand_xoshiro/src/xoshiro128starstar.rs +++ b/rand/rand_xoshiro/src/xoshiro128starstar.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::{next_u64_via_u32, fill_bytes_via_next}; use rand_core::le::read_u32_into; use rand_core::{SeedableRng, RngCore, Error}; @@ -19,6 +20,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro128starstar.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128StarStar { s: [u32; 4], } @@ -30,10 +32,7 @@ impl Xoshiro128StarStar { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoroshiro128StarStar; /// /// let rng1 = Xoroshiro128StarStar::seed_from_u64(0); @@ -41,7 +40,6 @@ impl Xoshiro128StarStar { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u32, self, [0x8764000b, 0xf542d2d3, 0x6fa035c3, 0x77f2db5b]); diff --git a/rand/rand_xoshiro/src/xoshiro256plus.rs b/rand/rand_xoshiro/src/xoshiro256plus.rs index 08da5a8..396f588 100644 --- a/rand/rand_xoshiro/src/xoshiro256plus.rs +++ b/rand/rand_xoshiro/src/xoshiro256plus.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; @@ -20,6 +21,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro256plus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256Plus { s: [u64; 4], } @@ -31,10 +33,7 @@ impl Xoshiro256Plus { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro256Plus; /// /// let rng1 = Xoshiro256Plus::seed_from_u64(0); @@ -42,7 +41,6 @@ impl Xoshiro256Plus { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [ diff --git a/rand/rand_xoshiro/src/xoshiro256starstar.rs b/rand/rand_xoshiro/src/xoshiro256starstar.rs index fc0a208..2cc2029 100644 --- a/rand/rand_xoshiro/src/xoshiro256starstar.rs +++ b/rand/rand_xoshiro/src/xoshiro256starstar.rs @@ -6,6 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; @@ -19,6 +20,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro256starstar.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256StarStar { s: [u64; 4], } @@ -30,10 +32,7 @@ impl Xoshiro256StarStar { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro256StarStar; /// /// let rng1 = Xoshiro256StarStar::seed_from_u64(0); @@ -41,7 +40,6 @@ impl Xoshiro256StarStar { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [ diff --git a/rand/rand_xoshiro/src/xoshiro512plus.rs b/rand/rand_xoshiro/src/xoshiro512plus.rs index fe982e4..4b589f2 100644 --- a/rand/rand_xoshiro/src/xoshiro512plus.rs +++ b/rand/rand_xoshiro/src/xoshiro512plus.rs @@ -6,11 +6,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; -use Seed512; +use crate::Seed512; /// A xoshiro512+ random number generator. /// @@ -22,6 +23,7 @@ use Seed512; /// reference source code](http://xoshiro.di.unimi.it/xoshiro512plus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512Plus { s: [u64; 8], } @@ -33,10 +35,7 @@ impl Xoshiro512Plus { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro512Plus; /// /// let rng1 = Xoshiro512Plus::seed_from_u64(0); @@ -44,7 +43,6 @@ impl Xoshiro512Plus { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [ diff --git a/rand/rand_xoshiro/src/xoshiro512starstar.rs b/rand/rand_xoshiro/src/xoshiro512starstar.rs index 1a33f0a..2db9ac1 100644 --- a/rand/rand_xoshiro/src/xoshiro512starstar.rs +++ b/rand/rand_xoshiro/src/xoshiro512starstar.rs @@ -6,11 +6,12 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; use rand_core::impls::fill_bytes_via_next; use rand_core::le::read_u64_into; use rand_core::{SeedableRng, RngCore, Error}; -use Seed512; +use crate::Seed512; /// A xoshiro512** random number generator. /// @@ -21,6 +22,7 @@ use Seed512; /// reference source code](http://xoshiro.di.unimi.it/xoshiro512starstar.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone)] +#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512StarStar { s: [u64; 8], } @@ -32,10 +34,7 @@ impl Xoshiro512StarStar { /// parallel computations. /// /// ``` - /// # extern crate rand; - /// # extern crate rand_xoshiro; - /// # fn main() { - /// use rand::SeedableRng; + /// use rand_xoshiro::rand_core::SeedableRng; /// use rand_xoshiro::Xoshiro512StarStar; /// /// let rng1 = Xoshiro512StarStar::seed_from_u64(0); @@ -43,7 +42,6 @@ impl Xoshiro512StarStar { /// rng2.jump(); /// let mut rng3 = rng2.clone(); /// rng3.jump(); - /// # } /// ``` pub fn jump(&mut self) { impl_jump!(u64, self, [ diff --git a/rand/rand_xoshiro/tests/serde.rs b/rand/rand_xoshiro/tests/serde.rs new file mode 100644 index 0000000..ee23a1d --- /dev/null +++ b/rand/rand_xoshiro/tests/serde.rs @@ -0,0 +1,83 @@ +#![cfg(feature="serde1")] + +use rand_core::{RngCore, SeedableRng}; +use rand_xoshiro::{SplitMix64, Xoroshiro64StarStar, Xoroshiro64Star, + Xoroshiro128Plus, Xoroshiro128StarStar, Xoshiro128StarStar, Xoshiro128Plus, + Xoshiro256StarStar, Xoshiro256Plus, Xoshiro512StarStar, Xoshiro512Plus}; + +macro_rules! serde_rng { + ($rng:ident) => { + use bincode; + use std::io::{BufWriter, BufReader}; + + let mut rng = $rng::seed_from_u64(0); + + let buf: Vec<u8> = Vec::new(); + let mut buf = BufWriter::new(buf); + bincode::serialize_into(&mut buf, &rng).expect("Could not serialize"); + + let buf = buf.into_inner().unwrap(); + let mut read = BufReader::new(&buf[..]); + let mut deserialized: $rng = bincode::deserialize_from(&mut read) + .expect("Could not deserialize"); + + for _ in 0..16 { + assert_eq!(rng.next_u64(), deserialized.next_u64()); + } + } +} + +#[test] +fn test_splitmix64() { + serde_rng!(SplitMix64); +} + +#[test] +fn test_xoroshiro64starstar() { + serde_rng!(Xoroshiro64StarStar); +} + +#[test] +fn test_xoroshiro64star() { + serde_rng!(Xoroshiro64Star); +} + +#[test] +fn test_xoroshiro128plus() { + serde_rng!(Xoroshiro128Plus); +} + +#[test] +fn test_xoroshiro128starstar() { + serde_rng!(Xoroshiro128StarStar); +} + +#[test] +fn test_xoshiro128starstar() { + serde_rng!(Xoshiro128StarStar); +} + +#[test] +fn test_xoshiro128plus() { + serde_rng!(Xoshiro128Plus); +} + +#[test] +fn test_xoshiro256starstar() { + serde_rng!(Xoshiro256StarStar); +} + +#[test] +fn test_xoshiro256plus() { + serde_rng!(Xoshiro256Plus); +} + +#[test] +fn test_xoshiro512starstar() { + serde_rng!(Xoshiro512StarStar); +} + +#[test] +fn test_xoshiro512plus() { + serde_rng!(Xoshiro512Plus); +} |