diff --git a/rand_hc/CHANGELOG.md b/rand_hc/CHANGELOG.md index f722e4c1..85543a9d 100644 --- a/rand_hc/CHANGELOG.md +++ b/rand_hc/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changes - Use Edition 2024 and MSRV 1.85 (#73) +- Update to `rand_core` v0.10 (#82) ## [0.4.0] - 2025-01-27 - Bump the MSRV to 1.63 (#58) diff --git a/rand_hc/Cargo.toml b/rand_hc/Cargo.toml index bd3ecfc5..aabe1468 100644 --- a/rand_hc/Cargo.toml +++ b/rand_hc/Cargo.toml @@ -16,4 +16,4 @@ edition = "2024" rust-version = "1.85" [dependencies] -rand_core = "0.10.0-rc-2" +rand_core = "0.10.0-rc-3" diff --git a/rand_hc/src/hc128.rs b/rand_hc/src/hc128.rs index fe6ba269..2353876e 100644 --- a/rand_hc/src/hc128.rs +++ b/rand_hc/src/hc128.rs @@ -15,8 +15,8 @@ //! The HC-128 random number generator. use core::fmt; -use rand_core::block::{BlockRng, BlockRngCore, CryptoBlockRng}; -use rand_core::{CryptoRng, RngCore, SeedableRng, le}; +use rand_core::block::{BlockRng, CryptoGenerator, Generator}; +use rand_core::{CryptoRng, RngCore, SeedableRng, utils}; const SEED_WORDS: usize = 8; // 128 bit key followed by 128 bit iv @@ -75,12 +75,12 @@ pub struct Hc128Rng(BlockRng); impl RngCore for Hc128Rng { #[inline] fn next_u32(&mut self) -> u32 { - self.0.next_u32() + self.0.next_word() } #[inline] fn next_u64(&mut self) -> u64 { - self.0.next_u64() + self.0.next_u64_from_u32() } #[inline] @@ -94,7 +94,7 @@ impl SeedableRng for Hc128Rng { #[inline] fn from_seed(seed: Self::Seed) -> Self { - Hc128Rng(BlockRng::::from_seed(seed)) + Hc128Rng(BlockRng::new(Hc128Core::from_seed(seed))) } } @@ -121,11 +121,10 @@ impl fmt::Debug for Hc128Core { } } -impl BlockRngCore for Hc128Core { - type Item = u32; - type Results = [u32; 16]; +impl Generator for Hc128Core { + type Output = [u32; 16]; - fn generate(&mut self, results: &mut Self::Results) { + fn generate(&mut self, results: &mut Self::Output) { assert!(self.counter1024 % 16 == 0); let cc = self.counter1024 % 512; @@ -336,13 +335,11 @@ impl SeedableRng for Hc128Core { /// 256 bits in length, matching the 128 bit `key` followed by 128 bit `iv` /// when HC-128 where to be used as a stream cipher. fn from_seed(seed: Self::Seed) -> Self { - let mut seed_u32 = [0u32; SEED_WORDS]; - le::read_u32_into(&seed, &mut seed_u32); - Self::init(seed_u32) + Self::init(utils::read_words(&seed)) } } -impl CryptoBlockRng for Hc128Core {} +impl CryptoGenerator for Hc128Core {} // Custom PartialEq implementation as it can't currently be derived from an array of size 1024 impl PartialEq for Hc128Core { diff --git a/rand_isaac/CHANGELOG.md b/rand_isaac/CHANGELOG.md index 42e19756..c34d3047 100644 --- a/rand_isaac/CHANGELOG.md +++ b/rand_isaac/CHANGELOG.md @@ -5,8 +5,12 @@ 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] +### Value-breaking changes +- Drop use of half-used words in `Isaac64Rng::next_u32` (#82) + ### Changes - Use Edition 2024 and MSRV 1.85 (#73) +- Update to `rand_core` v0.10 (#82) ## [0.4.0] - 2025-01-27 - Bump the MSRV to 1.63 (#58) diff --git a/rand_isaac/Cargo.toml b/rand_isaac/Cargo.toml index cf30165d..6af3a691 100644 --- a/rand_isaac/Cargo.toml +++ b/rand_isaac/Cargo.toml @@ -19,11 +19,12 @@ rust-version = "1.85" all-features = true [features] -serde = ["dep:serde", "rand_core/serde"] +serde = ["dep:serde", "dep:serde_arrays"] [dependencies] -rand_core = "0.10.0-rc-2" +rand_core = "0.10.0-rc-3" serde = { version = "1.0.104", features = ["derive"], optional = true } +serde_arrays = { version = "0.2.0", optional = true } [dev-dependencies] # This is for testing serde, unfortunately we can't specify feature-gated dev diff --git a/rand_isaac/src/isaac.rs b/rand_isaac/src/isaac.rs index 88771beb..ff882a04 100644 --- a/rand_isaac/src/isaac.rs +++ b/rand_isaac/src/isaac.rs @@ -9,11 +9,10 @@ //! The ISAAC random number generator. -use crate::isaac_array::IsaacArray; use core::num::Wrapping as w; use core::{fmt, slice}; -use rand_core::block::{BlockRng, BlockRngCore}; -use rand_core::{RngCore, SeedableRng, TryRngCore, le}; +use rand_core::block::{BlockRng, Generator}; +use rand_core::{RngCore, SeedableRng, TryRngCore, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -90,18 +89,17 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; /// /// [`rand_hc`]: https://docs.rs/rand_hc #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct IsaacRng(BlockRng); impl RngCore for IsaacRng { #[inline] fn next_u32(&mut self) -> u32 { - self.0.next_u32() + self.0.next_word() } #[inline] fn next_u64(&mut self) -> u64 { - self.0.next_u64() + self.0.next_u64_from_u32() } #[inline] @@ -115,7 +113,7 @@ impl SeedableRng for IsaacRng { #[inline] fn from_seed(seed: Self::Seed) -> Self { - IsaacRng(BlockRng::::from_seed(seed)) + IsaacRng(BlockRng::new(IsaacCore::from_seed(seed))) } /// Create an ISAAC random number generator using an `u64` as seed. @@ -123,7 +121,7 @@ impl SeedableRng for IsaacRng { /// the reference implementation when used unseeded. #[inline] fn seed_from_u64(seed: u64) -> Self { - IsaacRng(BlockRng::::seed_from_u64(seed)) + IsaacRng(BlockRng::new(IsaacCore::seed_from_u64(seed))) } #[inline] @@ -131,7 +129,7 @@ impl SeedableRng for IsaacRng { where R: RngCore + ?Sized, { - IsaacRng(BlockRng::::from_rng(rng)) + IsaacRng(BlockRng::new(IsaacCore::from_rng(rng))) } #[inline] @@ -139,7 +137,133 @@ impl SeedableRng for IsaacRng { where S: TryRngCore + ?Sized, { - BlockRng::::try_from_rng(rng).map(IsaacRng) + IsaacCore::try_from_rng(rng).map(|core| IsaacRng(BlockRng::new(core))) + } +} + +#[cfg(feature = "serde")] +mod serde_impls { + use super::{IsaacCore, IsaacRng, RAND_SIZE}; + use core::fmt; + use rand_core::block::BlockRng; + use serde::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; + use serde::ser::{Serialize, SerializeStruct, Serializer}; + + impl Serialize for IsaacRng { + fn serialize(&self, serializer: S) -> Result { + let mut state = serializer.serialize_struct("IsaacRng", 2)?; + state.serialize_field("core", &self.0.core)?; + state.serialize_field("results", self.0.remaining_results())?; + state.end() + } + } + + struct Results { + results: [u32; RAND_SIZE], + len: usize, + } + impl Results { + fn to_rng(&self, core: IsaacCore) -> IsaacRng { + let results = &self.results[..self.len]; + IsaacRng(BlockRng::reconstruct(core, results).unwrap()) + } + } + struct ResultsVisitor; + impl<'de> Visitor<'de> for ResultsVisitor { + type Value = Results; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "") // TODO + } + + fn visit_seq>(self, mut seq: A) -> Result { + let mut results = [0u32; RAND_SIZE]; + let mut len = 0; + while let Some(value) = seq.next_element()? { + if len >= results.len() { + return Err(Error::invalid_length( + len + 1, + &("up to 256 elements" as &str), + )); + } + + results[len] = value; + len += 1; + } + + Ok(Results { results, len }) + } + } + + impl<'de> Deserialize<'de> for Results { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_seq(ResultsVisitor) + } + } + + #[derive(serde::Deserialize)] + #[serde(field_identifier, rename_all = "lowercase")] + enum Field { + Core, + Results, + } + + struct IsaacVisitor; + impl<'de> Visitor<'de> for IsaacVisitor { + type Value = IsaacRng; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "") // TODO + } + + fn visit_seq(self, mut seq: V) -> Result + where + V: SeqAccess<'de>, + { + let core = seq + .next_element()? + .ok_or_else(|| Error::invalid_length(0, &self))?; + let results: Results = seq + .next_element()? + .ok_or_else(|| Error::invalid_length(1, &self))?; + + Ok(results.to_rng(core)) + } + + fn visit_map(self, mut map: V) -> Result + where + V: MapAccess<'de>, + { + let mut core = None; + let mut results: Option = None; + while let Some(key) = map.next_key()? { + match key { + Field::Core => { + if core.is_some() { + return Err(Error::duplicate_field("core")); + } + core = Some(map.next_value()?); + } + Field::Results => { + if results.is_some() { + return Err(Error::duplicate_field("results")); + } + results = Some(map.next_value()?); + } + } + } + let core = core.ok_or_else(|| Error::missing_field("core"))?; + let results = results.ok_or_else(|| Error::missing_field("results"))?; + + Ok(results.to_rng(core)) + } + } + + impl<'de> Deserialize<'de> for IsaacRng { + fn deserialize>(deserializer: D) -> Result { + const FIELDS: &[&str] = &["core", "results"]; + deserializer.deserialize_struct("IsaacRng", FIELDS, IsaacVisitor) + } } } @@ -147,10 +271,7 @@ impl SeedableRng for IsaacRng { #[derive(Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct IsaacCore { - #[cfg_attr( - feature = "serde", - serde(with = "super::isaac_array::isaac_array_serde") - )] + #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))] mem: [w32; RAND_SIZE], a: w32, b: w32, @@ -174,9 +295,8 @@ impl ::core::cmp::PartialEq for IsaacCore { // Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE impl ::core::cmp::Eq for IsaacCore {} -impl BlockRngCore for IsaacCore { - type Item = u32; - type Results = IsaacArray; +impl Generator for IsaacCore { + type Output = [u32; RAND_SIZE]; /// Refills the output buffer, `results`. See also the pseudocode description /// of the algorithm in the `IsaacRng` documentation. @@ -198,7 +318,7 @@ impl BlockRngCore for IsaacCore { /// make `fill_bytes` a memcopy. To maintain compatibility we fill in /// reverse. #[rustfmt::skip] - fn generate(&mut self, results: &mut IsaacArray) { + fn generate(&mut self, results: &mut [u32; RAND_SIZE]) { self.c += w(1); // abbreviations let mut a = self.a; @@ -345,8 +465,7 @@ impl SeedableRng for IsaacCore { type Seed = [u8; 32]; fn from_seed(seed: Self::Seed) -> Self { - let mut seed_u32 = [0u32; 8]; - le::read_u32_into(&seed, &mut seed_u32); + let seed_u32: [u32; 8] = utils::read_words(&seed); // Convert the seed to `Wrapping` and zero-extend to `RAND_SIZE`. let mut seed_extended = [w(0); RAND_SIZE]; for (x, y) in seed_extended.iter_mut().zip(seed_u32.iter()) { @@ -554,6 +673,10 @@ mod test { ]; let mut rng = IsaacRng::from_seed(seed); + // discard some results + let _ = rng.next_u64(); + let _ = rng.next_u32(); + let buf: Vec = Vec::new(); let mut buf = BufWriter::new(buf); bincode::serialize_into(&mut buf, &rng).expect("Could not serialize"); diff --git a/rand_isaac/src/isaac64.rs b/rand_isaac/src/isaac64.rs index 713cf0df..d24d5977 100644 --- a/rand_isaac/src/isaac64.rs +++ b/rand_isaac/src/isaac64.rs @@ -9,11 +9,10 @@ //! The ISAAC-64 random number generator. -use crate::isaac_array::IsaacArray; use core::num::Wrapping as w; use core::{fmt, slice}; -use rand_core::block::{BlockRng64, BlockRngCore}; -use rand_core::{RngCore, SeedableRng, TryRngCore, le}; +use rand_core::block::{BlockRng, Generator}; +use rand_core::{RngCore, SeedableRng, TryRngCore, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -70,7 +69,7 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; /// } /// ``` /// -/// This implementation uses [`BlockRng64`] to implement the [`RngCore`] methods. +/// This implementation uses [`BlockRng`] to implement the [`RngCore`] methods. /// /// See for more information the documentation of [`IsaacRng`]. /// @@ -79,20 +78,19 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; /// /// [`IsaacRng`]: crate::isaac::IsaacRng /// [`rand_hc`]: https://docs.rs/rand_hc -/// [`BlockRng64`]: rand_core::block::BlockRng64 +/// [`BlockRng`]: rand_core::block::BlockRng #[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Isaac64Rng(BlockRng64); +pub struct Isaac64Rng(BlockRng); impl RngCore for Isaac64Rng { #[inline] fn next_u32(&mut self) -> u32 { - self.0.next_u32() + self.0.next_word() as u32 } #[inline] fn next_u64(&mut self) -> u64 { - self.0.next_u64() + self.0.next_word() } #[inline] @@ -106,7 +104,7 @@ impl SeedableRng for Isaac64Rng { #[inline] fn from_seed(seed: Self::Seed) -> Self { - Isaac64Rng(BlockRng64::::from_seed(seed)) + Isaac64Rng(BlockRng::new(Isaac64Core::from_seed(seed))) } /// Create an ISAAC random number generator using an `u64` as seed. @@ -114,7 +112,7 @@ impl SeedableRng for Isaac64Rng { /// the reference implementation when used unseeded. #[inline] fn seed_from_u64(seed: u64) -> Self { - Isaac64Rng(BlockRng64::::seed_from_u64(seed)) + Isaac64Rng(BlockRng::new(Isaac64Core::seed_from_u64(seed))) } #[inline] @@ -122,7 +120,7 @@ impl SeedableRng for Isaac64Rng { where R: RngCore + ?Sized, { - Isaac64Rng(BlockRng64::::from_rng(rng)) + Isaac64Rng(BlockRng::new(Isaac64Core::from_rng(rng))) } #[inline] @@ -130,7 +128,133 @@ impl SeedableRng for Isaac64Rng { where S: TryRngCore + ?Sized, { - BlockRng64::::try_from_rng(rng).map(Isaac64Rng) + Isaac64Core::try_from_rng(rng).map(|core| Isaac64Rng(BlockRng::new(core))) + } +} + +#[cfg(feature = "serde")] +mod serde_impls { + use super::{Isaac64Core, Isaac64Rng, RAND_SIZE}; + use core::fmt; + use rand_core::block::BlockRng; + use serde::de::{Deserialize, Deserializer, Error, MapAccess, SeqAccess, Visitor}; + use serde::ser::{Serialize, SerializeStruct, Serializer}; + + impl Serialize for Isaac64Rng { + fn serialize(&self, serializer: S) -> Result { + let mut state = serializer.serialize_struct("Isaac64Rng", 2)?; + state.serialize_field("core", &self.0.core)?; + state.serialize_field("results", self.0.remaining_results())?; + state.end() + } + } + + struct Results { + results: [u64; RAND_SIZE], + len: usize, + } + impl Results { + fn to_rng(&self, core: Isaac64Core) -> Isaac64Rng { + let results = &self.results[..self.len]; + Isaac64Rng(BlockRng::reconstruct(core, results).unwrap()) + } + } + struct ResultsVisitor; + impl<'de> Visitor<'de> for ResultsVisitor { + type Value = Results; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "") // TODO + } + + fn visit_seq>(self, mut seq: A) -> Result { + let mut results = [0u64; RAND_SIZE]; + let mut len = 0; + while let Some(value) = seq.next_element()? { + if len >= results.len() { + return Err(Error::invalid_length( + len + 1, + &("up to 256 elements" as &str), + )); + } + + results[len] = value; + len += 1; + } + + Ok(Results { results, len }) + } + } + + impl<'de> Deserialize<'de> for Results { + fn deserialize>(deserializer: D) -> Result { + deserializer.deserialize_seq(ResultsVisitor) + } + } + + #[derive(serde::Deserialize)] + #[serde(field_identifier, rename_all = "lowercase")] + enum Field { + Core, + Results, + } + + struct IsaacVisitor; + impl<'de> Visitor<'de> for IsaacVisitor { + type Value = Isaac64Rng; + + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(formatter, "") // TODO + } + + fn visit_seq(self, mut seq: V) -> Result + where + V: SeqAccess<'de>, + { + let core = seq + .next_element()? + .ok_or_else(|| Error::invalid_length(0, &self))?; + let results: Results = seq + .next_element()? + .ok_or_else(|| Error::invalid_length(1, &self))?; + + Ok(results.to_rng(core)) + } + + fn visit_map(self, mut map: V) -> Result + where + V: MapAccess<'de>, + { + let mut core = None; + let mut results: Option = None; + while let Some(key) = map.next_key()? { + match key { + Field::Core => { + if core.is_some() { + return Err(Error::duplicate_field("core")); + } + core = Some(map.next_value()?); + } + Field::Results => { + if results.is_some() { + return Err(Error::duplicate_field("results")); + } + results = Some(map.next_value()?); + } + } + } + let core = core.ok_or_else(|| Error::missing_field("core"))?; + let results = results.ok_or_else(|| Error::missing_field("results"))?; + + Ok(results.to_rng(core)) + } + } + + impl<'de> Deserialize<'de> for Isaac64Rng { + fn deserialize>(deserializer: D) -> Result { + const FIELDS: &[&str] = &["core", "results"]; + deserializer.deserialize_struct("Isaac64Rng", FIELDS, IsaacVisitor) + } } } @@ -138,10 +262,7 @@ impl SeedableRng for Isaac64Rng { #[derive(Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Isaac64Core { - #[cfg_attr( - feature = "serde", - serde(with = "super::isaac_array::isaac_array_serde") - )] + #[cfg_attr(feature = "serde", serde(with = "serde_arrays"))] mem: [w64; RAND_SIZE], a: w64, b: w64, @@ -165,9 +286,8 @@ impl ::core::cmp::PartialEq for Isaac64Core { // Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE impl ::core::cmp::Eq for Isaac64Core {} -impl BlockRngCore for Isaac64Core { - type Item = u64; - type Results = IsaacArray; +impl Generator for Isaac64Core { + type Output = [u64; RAND_SIZE]; /// Refills the output buffer, `results`. See also the pseudocode description /// of the algorithm in the `Isaac64Rng` documentation. @@ -189,7 +309,7 @@ impl BlockRngCore for Isaac64Core { /// make `fill_bytes` a memcopy. To maintain compatibility we fill in /// reverse. #[rustfmt::skip] - fn generate(&mut self, results: &mut IsaacArray) { + fn generate(&mut self, results: &mut [u64; RAND_SIZE]) { self.c += w(1); // abbreviations let mut a = self.a; @@ -311,8 +431,7 @@ impl SeedableRng for Isaac64Core { type Seed = [u8; 32]; fn from_seed(seed: Self::Seed) -> Self { - let mut seed_u64 = [0u64; 4]; - le::read_u64_into(&seed, &mut seed_u64); + let seed_u64: [u64; 4] = utils::read_words(&seed); // Convert the seed to `Wrapping` and zero-extend to `RAND_SIZE`. let mut seed_extended = [w(0); RAND_SIZE]; for (x, y) in seed_extended.iter_mut().zip(seed_u64.iter()) { @@ -457,8 +576,8 @@ mod test { } // Subset of above values, as an LE u32 sequence let expected = [ - 3477963620, 3509106075, 687845478, 1797495790, 227048253, 2523132918, 4044335064, - 1260557630, 4079741768, 3001306521, 69157722, 3958365844, + 3477963620, 687845478, 227048253, 4044335064, 4079741768, 69157722, 3912394646, + 1204022051, 2459090310, 2151271855, 384864925, 1183723065, ]; assert_eq!(results, expected); } @@ -475,12 +594,12 @@ mod test { // `test_isaac64_true_values_32`. assert_eq!(rng.next_u64(), 15071495833797886820); assert_eq!(rng.next_u32(), 687845478); - assert_eq!(rng.next_u32(), 1797495790); - assert_eq!(rng.next_u64(), 10836773366498097981); - assert_eq!(rng.next_u32(), 4044335064); + assert_eq!(rng.next_u32(), 227048253); + assert_eq!(rng.next_u64(), 5414053799617603544); + assert_eq!(rng.next_u32(), 4079741768); // Skip one u32 - assert_eq!(rng.next_u64(), 12890513357046278984); - assert_eq!(rng.next_u32(), 69157722); + assert_eq!(rng.next_u64(), 17001051845652595546); + assert_eq!(rng.next_u32(), 3912394646); } #[test] @@ -503,7 +622,7 @@ mod test { #[test] fn test_isaac64_new_uninitialized() { - // Compare the results from initializing `IsaacRng` with + // Compare the results from initializing `Isaac64Rng` with // `seed_from_u64(0)`, to make sure it is the same as the reference // implementation when used uninitialized. // Note: We only test the first 16 integers, not the full 256 of the @@ -559,6 +678,10 @@ mod test { ]; let mut rng = Isaac64Rng::from_seed(seed); + // discard some results + let _ = rng.next_u64(); + let _ = rng.next_u32(); + let buf: Vec = Vec::new(); let mut buf = BufWriter::new(buf); bincode::serialize_into(&mut buf, &rng).expect("Could not serialize"); diff --git a/rand_isaac/src/isaac_array.rs b/rand_isaac/src/isaac_array.rs deleted file mode 100644 index afa5a160..00000000 --- a/rand_isaac/src/isaac_array.rs +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright 2018 Developers of the Rand project. -// Copyright 2017-2018 The Rust Project Developers. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! ISAAC helper functions for 256-element arrays. - -// Terrible workaround because arrays with more than 32 elements do not -// implement `AsRef`, `Default`, `Serialize`, `Deserialize`, or any other -// traits for that matter. - -#[cfg(feature = "serde")] -use serde::{Deserialize, Serialize}; - -const RAND_SIZE_LEN: usize = 8; -const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; - -#[derive(Copy, Clone)] -#[allow(missing_debug_implementations)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct IsaacArray { - #[cfg_attr(feature = "serde", serde(with = "isaac_array_serde"))] - #[cfg_attr( - feature = "serde", - serde(bound( - serialize = "T: Serialize", - deserialize = "T: Deserialize<'de> + Copy + Default" - )) - )] - inner: [T; RAND_SIZE], -} - -impl ::core::convert::AsRef<[T]> for IsaacArray { - #[inline(always)] - fn as_ref(&self) -> &[T] { - &self.inner[..] - } -} - -impl ::core::convert::AsMut<[T]> for IsaacArray { - #[inline(always)] - fn as_mut(&mut self) -> &mut [T] { - &mut self.inner[..] - } -} - -impl ::core::ops::Deref for IsaacArray { - type Target = [T; RAND_SIZE]; - #[inline(always)] - fn deref(&self) -> &Self::Target { - &self.inner - } -} - -impl ::core::ops::DerefMut for IsaacArray { - #[inline(always)] - fn deref_mut(&mut self) -> &mut [T; RAND_SIZE] { - &mut self.inner - } -} - -impl ::core::default::Default for IsaacArray -where - T: Copy + Default, -{ - fn default() -> IsaacArray { - IsaacArray { - inner: [T::default(); RAND_SIZE], - } - } -} - -// Custom PartialEq implementation as it can't currently be derived from an array of size RAND_SIZE -impl ::core::cmp::PartialEq for IsaacArray -where - T: PartialEq, -{ - fn eq(&self, other: &IsaacArray) -> bool { - self.inner[..] == other.inner[..] - } -} - -// Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE -impl ::core::cmp::Eq for IsaacArray where T: Eq {} - -#[cfg(feature = "serde")] -pub(super) mod isaac_array_serde { - const RAND_SIZE_LEN: usize = 8; - const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; - - use serde::de; - use serde::de::{SeqAccess, Visitor}; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - use core::fmt; - - pub fn serialize(arr: &[T; RAND_SIZE], ser: S) -> Result - where - T: Serialize, - S: Serializer, - { - use serde::ser::SerializeTuple; - - let mut seq = ser.serialize_tuple(RAND_SIZE)?; - - for e in arr.iter() { - seq.serialize_element(&e)?; - } - - seq.end() - } - - #[inline] - pub fn deserialize<'de, T, D>(de: D) -> Result<[T; RAND_SIZE], D::Error> - where - T: Deserialize<'de> + Default + Copy, - D: Deserializer<'de>, - { - use core::marker::PhantomData; - struct ArrayVisitor { - _pd: PhantomData, - } - impl<'de, T> Visitor<'de> for ArrayVisitor - where - T: Deserialize<'de> + Default + Copy, - { - type Value = [T; RAND_SIZE]; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("Isaac state array") - } - - #[inline] - fn visit_seq(self, mut seq: A) -> Result<[T; RAND_SIZE], A::Error> - where - A: SeqAccess<'de>, - { - let mut out = [Default::default(); RAND_SIZE]; - - for i in 0..RAND_SIZE { - match seq.next_element()? { - Some(val) => out[i] = val, - None => return Err(de::Error::invalid_length(i, &self)), - }; - } - - Ok(out) - } - } - - de.deserialize_tuple(RAND_SIZE, ArrayVisitor { _pd: PhantomData }) - } -} diff --git a/rand_isaac/src/lib.rs b/rand_isaac/src/lib.rs index 2f00d608..ca5524e9 100644 --- a/rand_isaac/src/lib.rs +++ b/rand_isaac/src/lib.rs @@ -27,7 +27,5 @@ pub mod isaac; pub mod isaac64; -mod isaac_array; - pub use self::isaac::IsaacRng; pub use self::isaac64::Isaac64Rng; diff --git a/rand_jitter/CHANGELOG.md b/rand_jitter/CHANGELOG.md index bc11dc4d..536942ef 100644 --- a/rand_jitter/CHANGELOG.md +++ b/rand_jitter/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changes - Use Edition 2024 and MSRV 1.85 (#73) +- Update to `rand_core` v0.10 (#82) ## [0.5.0] - 2025-01-27 - Bump the MSRV to 1.63 (#58) diff --git a/rand_jitter/Cargo.toml b/rand_jitter/Cargo.toml index 47430526..0e601264 100644 --- a/rand_jitter/Cargo.toml +++ b/rand_jitter/Cargo.toml @@ -19,7 +19,7 @@ std = [] log = ["dep:log"] [dependencies] -rand_core = "0.10.0-rc-2" +rand_core = "0.10.0-rc-3" log = { version = "0.4.4", optional = true } [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] diff --git a/rand_jitter/src/lib.rs b/rand_jitter/src/lib.rs index 1e864104..e323878a 100644 --- a/rand_jitter/src/lib.rs +++ b/rand_jitter/src/lib.rs @@ -106,7 +106,7 @@ mod error; mod platform; pub use crate::error::TimerError; -use rand_core::{RngCore, le}; +use rand_core::{RngCore, utils}; use core::{fmt, mem, ptr}; #[cfg(feature = "std")] @@ -760,6 +760,6 @@ where // // This is done especially for wrappers that implement `next_u32` // themselves via `fill_bytes`. - le::fill_bytes_via_next(self, dest) + utils::fill_bytes_via_next_word(dest, || self.next_u64()) } } diff --git a/rand_sfc/CHANGELOG.md b/rand_sfc/CHANGELOG.md index af0b082c..b819b458 100644 --- a/rand_sfc/CHANGELOG.md +++ b/rand_sfc/CHANGELOG.md @@ -13,6 +13,7 @@ Compared to the prior implementation: ### Changed - Value-stability is not preserved since constructors use a different number of mixing rounds +- Update to `rand_core` v0.10 (#82) ### Removed diff --git a/rand_sfc/Cargo.toml b/rand_sfc/Cargo.toml index f20a9820..e94eee79 100644 --- a/rand_sfc/Cargo.toml +++ b/rand_sfc/Cargo.toml @@ -19,5 +19,5 @@ all-features = true serde = ["dep:serde"] [dependencies] -rand_core = { version = "0.10.0-rc-2" } +rand_core = { version = "0.10.0-rc-3" } serde = { version = "1.0.118", default-features = false, features = ["derive"], optional = true } diff --git a/rand_sfc/src/sfc32.rs b/rand_sfc/src/sfc32.rs index 42e2023e..7a8afe66 100644 --- a/rand_sfc/src/sfc32.rs +++ b/rand_sfc/src/sfc32.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -55,12 +54,12 @@ impl RngCore for Sfc32 { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } @@ -73,8 +72,7 @@ impl SeedableRng for Sfc32 { /// Create a new `Sfc32`. fn from_seed(seed: [u8; 12]) -> Sfc32 { - let mut s = [0; 3]; - read_u32_into(&seed, &mut s); + let s: [_; 3] = utils::read_words(&seed); let mut rng = Sfc32 { a: s[0], diff --git a/rand_sfc/src/sfc64.rs b/rand_sfc/src/sfc64.rs index e1da7388..e3598c3b 100644 --- a/rand_sfc/src/sfc64.rs +++ b/rand_sfc/src/sfc64.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -63,7 +62,7 @@ impl RngCore for Sfc64 { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } @@ -76,8 +75,7 @@ impl SeedableRng for Sfc64 { /// Create a new `Sfc64`. fn from_seed(seed: [u8; 24]) -> Sfc64 { - let mut s = [0; 3]; - read_u64_into(&seed, &mut s); + let s: [_; 3] = utils::read_words(&seed); let mut rng = Sfc64 { a: s[0], diff --git a/rand_xorshift/CHANGELOG.md b/rand_xorshift/CHANGELOG.md index 07dadb90..9f32ecb8 100644 --- a/rand_xorshift/CHANGELOG.md +++ b/rand_xorshift/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changes - Use Edition 2024 and MSRV 1.85 (#73) +- Update to `rand_core` v0.10 (#82) ## [0.4.0] - 2025-01-27 - Bump the MSRV to 1.63 (#58) diff --git a/rand_xorshift/Cargo.toml b/rand_xorshift/Cargo.toml index 76d7c15b..68ecf48d 100644 --- a/rand_xorshift/Cargo.toml +++ b/rand_xorshift/Cargo.toml @@ -22,7 +22,7 @@ all-features = true serde = ["dep:serde"] [dependencies] -rand_core = "0.10.0-rc-2" +rand_core = "0.10.0-rc-3" serde = { version = "1.0.118", default-features = false, features = ["derive"], optional = true } [dev-dependencies] diff --git a/rand_xorshift/src/lib.rs b/rand_xorshift/src/lib.rs index 188d31b8..66032faa 100644 --- a/rand_xorshift/src/lib.rs +++ b/rand_xorshift/src/lib.rs @@ -31,7 +31,7 @@ use core::fmt; use core::num::Wrapping as w; -use rand_core::{RngCore, SeedableRng, TryRngCore, le}; +use rand_core::{RngCore, SeedableRng, TryRngCore, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -82,12 +82,12 @@ impl RngCore for XorShiftRng { #[inline] fn next_u64(&mut self) -> u64 { - le::next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - le::fill_bytes_via_next(self, dest) + utils::fill_bytes_via_next_word(dest, || self.next_u32()) } } @@ -95,8 +95,7 @@ impl SeedableRng for XorShiftRng { type Seed = [u8; 16]; fn from_seed(seed: Self::Seed) -> Self { - let mut seed_u32 = [0u32; 4]; - le::read_u32_into(&seed, &mut seed_u32); + let mut seed_u32: [u32; 4] = utils::read_words(&seed); // Xorshift cannot be seeded with 0 and we cannot return an Error, but // also do not wish to panic (because a random seed can legitimately be diff --git a/rand_xoshiro/CHANGELOG.md b/rand_xoshiro/CHANGELOG.md index 372e6628..0391ca98 100644 --- a/rand_xoshiro/CHANGELOG.md +++ b/rand_xoshiro/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Changes - Use Edition 2024 and MSRV 1.85 (#73) +- Update to `rand_core` v0.10 (#82) ## [0.7.0] - 2025-01-27 - Bump the MSRV to 1.63 (#58) diff --git a/rand_xoshiro/Cargo.toml b/rand_xoshiro/Cargo.toml index 902cac69..543eac71 100644 --- a/rand_xoshiro/Cargo.toml +++ b/rand_xoshiro/Cargo.toml @@ -20,7 +20,7 @@ all-features = true serde = ["dep:serde"] [dependencies] -rand_core = "0.10.0-rc-2" +rand_core = "0.10.0-rc-3" serde = { version = "1", features = ["derive"], optional=true } [dev-dependencies] diff --git a/rand_xoshiro/src/splitmix64.rs b/rand_xoshiro/src/splitmix64.rs index 535cc32c..849ae85b 100644 --- a/rand_xoshiro/src/splitmix64.rs +++ b/rand_xoshiro/src/splitmix64.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -54,7 +53,7 @@ impl RngCore for SplitMix64 { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } @@ -63,8 +62,7 @@ impl SeedableRng for SplitMix64 { /// Create a new `SplitMix64`. fn from_seed(seed: [u8; 8]) -> SplitMix64 { - let mut state = [0; 1]; - read_u64_into(&seed, &mut state); + let state: [_; 1] = utils::read_words(&seed); SplitMix64 { x: state[0] } } diff --git a/rand_xoshiro/src/xoroshiro128plus.rs b/rand_xoshiro/src/xoroshiro128plus.rs index dfb927e5..fe4f5400 100644 --- a/rand_xoshiro/src/xoroshiro128plus.rs +++ b/rand_xoshiro/src/xoroshiro128plus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -75,7 +74,7 @@ impl RngCore for Xoroshiro128Plus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } impl SeedableRng for Xoroshiro128Plus { @@ -85,8 +84,7 @@ impl SeedableRng for Xoroshiro128Plus { /// mapped to a different seed. fn from_seed(seed: [u8; 16]) -> Xoroshiro128Plus { deal_with_zero_seed!(seed, Self, 16); - let mut s = [0; 2]; - read_u64_into(&seed, &mut s); + let s: [_; 2] = utils::read_words(&seed); Xoroshiro128Plus { s0: s[0], s1: s[1] } } diff --git a/rand_xoshiro/src/xoroshiro128plusplus.rs b/rand_xoshiro/src/xoroshiro128plusplus.rs index d10934d8..bce5e89e 100644 --- a/rand_xoshiro/src/xoroshiro128plusplus.rs +++ b/rand_xoshiro/src/xoroshiro128plusplus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -72,7 +71,7 @@ impl RngCore for Xoroshiro128PlusPlus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } @@ -83,8 +82,7 @@ impl SeedableRng for Xoroshiro128PlusPlus { /// mapped to a different seed. fn from_seed(seed: [u8; 16]) -> Xoroshiro128PlusPlus { deal_with_zero_seed!(seed, Self, 16); - let mut s = [0; 2]; - read_u64_into(&seed, &mut s); + let s: [_; 2] = utils::read_words(&seed); Xoroshiro128PlusPlus { s0: s[0], s1: s[1] } } diff --git a/rand_xoshiro/src/xoroshiro128starstar.rs b/rand_xoshiro/src/xoroshiro128starstar.rs index 30972f50..cad407a5 100644 --- a/rand_xoshiro/src/xoroshiro128starstar.rs +++ b/rand_xoshiro/src/xoroshiro128starstar.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -72,7 +71,7 @@ impl RngCore for Xoroshiro128StarStar { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } @@ -83,8 +82,7 @@ impl SeedableRng for Xoroshiro128StarStar { /// mapped to a different seed. fn from_seed(seed: [u8; 16]) -> Xoroshiro128StarStar { deal_with_zero_seed!(seed, Self, 16); - let mut s = [0; 2]; - read_u64_into(&seed, &mut s); + let s: [_; 2] = utils::read_words(&seed); Xoroshiro128StarStar { s0: s[0], s1: s[1] } } diff --git a/rand_xoshiro/src/xoroshiro64star.rs b/rand_xoshiro/src/xoroshiro64star.rs index f6e86d86..2c9ef378 100644 --- a/rand_xoshiro/src/xoroshiro64star.rs +++ b/rand_xoshiro/src/xoroshiro64star.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -38,12 +37,12 @@ impl RngCore for Xoroshiro64Star { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } @@ -54,8 +53,7 @@ impl SeedableRng for Xoroshiro64Star { /// mapped to a different seed. fn from_seed(seed: [u8; 8]) -> Xoroshiro64Star { deal_with_zero_seed!(seed, Self, 8); - let mut s = [0; 2]; - read_u32_into(&seed, &mut s); + let s: [_; 2] = utils::read_words(&seed); Xoroshiro64Star { s0: s[0], s1: s[1] } } diff --git a/rand_xoshiro/src/xoroshiro64starstar.rs b/rand_xoshiro/src/xoroshiro64starstar.rs index bfce708d..84ea6a1b 100644 --- a/rand_xoshiro/src/xoroshiro64starstar.rs +++ b/rand_xoshiro/src/xoroshiro64starstar.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -37,12 +36,12 @@ impl RngCore for Xoroshiro64StarStar { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } @@ -53,8 +52,7 @@ impl SeedableRng for Xoroshiro64StarStar { /// mapped to a different seed. fn from_seed(seed: [u8; 8]) -> Xoroshiro64StarStar { deal_with_zero_seed!(seed, Self, 8); - let mut s = [0; 2]; - read_u32_into(&seed, &mut s); + let s: [_; 2] = utils::read_words(&seed); Xoroshiro64StarStar { s0: s[0], s1: s[1] } } diff --git a/rand_xoshiro/src/xoshiro128plus.rs b/rand_xoshiro/src/xoshiro128plus.rs index b6c53883..1bbeb9a9 100644 --- a/rand_xoshiro/src/xoshiro128plus.rs +++ b/rand_xoshiro/src/xoshiro128plus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -64,9 +63,9 @@ impl SeedableRng for Xoshiro128Plus { #[inline] fn from_seed(seed: [u8; 16]) -> Xoshiro128Plus { deal_with_zero_seed!(seed, Self, 16); - let mut state = [0; 4]; - read_u32_into(&seed, &mut state); - Xoshiro128Plus { s: state } + Xoshiro128Plus { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro128Plus` from a `u64` using `SplitMix64`. @@ -85,12 +84,12 @@ impl RngCore for Xoshiro128Plus { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } diff --git a/rand_xoshiro/src/xoshiro128plusplus.rs b/rand_xoshiro/src/xoshiro128plusplus.rs index 793bee75..36f2d7df 100644 --- a/rand_xoshiro/src/xoshiro128plusplus.rs +++ b/rand_xoshiro/src/xoshiro128plusplus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -63,9 +62,9 @@ impl SeedableRng for Xoshiro128PlusPlus { #[inline] fn from_seed(seed: [u8; 16]) -> Xoshiro128PlusPlus { deal_with_zero_seed!(seed, Self, 16); - let mut state = [0; 4]; - read_u32_into(&seed, &mut state); - Xoshiro128PlusPlus { s: state } + Xoshiro128PlusPlus { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro128PlusPlus` from a `u64` using `SplitMix64`. @@ -84,12 +83,12 @@ impl RngCore for Xoshiro128PlusPlus { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } diff --git a/rand_xoshiro/src/xoshiro128starstar.rs b/rand_xoshiro/src/xoshiro128starstar.rs index 67b38b34..bc20e7f1 100644 --- a/rand_xoshiro/src/xoshiro128starstar.rs +++ b/rand_xoshiro/src/xoshiro128starstar.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, next_u64_via_u32, read_u32_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -63,9 +62,9 @@ impl SeedableRng for Xoshiro128StarStar { #[inline] fn from_seed(seed: [u8; 16]) -> Xoshiro128StarStar { deal_with_zero_seed!(seed, Self, 16); - let mut state = [0; 4]; - read_u32_into(&seed, &mut state); - Xoshiro128StarStar { s: state } + Xoshiro128StarStar { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro128StarStar` from a `u64` using `SplitMix64`. @@ -84,12 +83,12 @@ impl RngCore for Xoshiro128StarStar { #[inline] fn next_u64(&mut self) -> u64 { - next_u64_via_u32(self) + utils::next_u64_via_u32(self) } #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u32()); } } diff --git a/rand_xoshiro/src/xoshiro256plus.rs b/rand_xoshiro/src/xoshiro256plus.rs index c1f4b15b..17e52ecd 100644 --- a/rand_xoshiro/src/xoshiro256plus.rs +++ b/rand_xoshiro/src/xoshiro256plus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -82,9 +81,9 @@ impl SeedableRng for Xoshiro256Plus { #[inline] fn from_seed(seed: [u8; 32]) -> Xoshiro256Plus { deal_with_zero_seed!(seed, Self); - let mut state = [0; 4]; - read_u64_into(&seed, &mut state); - Xoshiro256Plus { s: state } + Xoshiro256Plus { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro256Plus` from a `u64` using `SplitMix64`. @@ -110,7 +109,7 @@ impl RngCore for Xoshiro256Plus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } diff --git a/rand_xoshiro/src/xoshiro256plusplus.rs b/rand_xoshiro/src/xoshiro256plusplus.rs index ed1aee6c..fd65777b 100644 --- a/rand_xoshiro/src/xoshiro256plusplus.rs +++ b/rand_xoshiro/src/xoshiro256plusplus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -81,9 +80,9 @@ impl SeedableRng for Xoshiro256PlusPlus { #[inline] fn from_seed(seed: [u8; 32]) -> Xoshiro256PlusPlus { deal_with_zero_seed!(seed, Self); - let mut state = [0; 4]; - read_u64_into(&seed, &mut state); - Xoshiro256PlusPlus { s: state } + Xoshiro256PlusPlus { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro256PlusPlus` from a `u64` using `SplitMix64`. @@ -109,7 +108,7 @@ impl RngCore for Xoshiro256PlusPlus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } diff --git a/rand_xoshiro/src/xoshiro256starstar.rs b/rand_xoshiro/src/xoshiro256starstar.rs index 650505ab..ab4b380f 100644 --- a/rand_xoshiro/src/xoshiro256starstar.rs +++ b/rand_xoshiro/src/xoshiro256starstar.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -81,9 +80,9 @@ impl SeedableRng for Xoshiro256StarStar { #[inline] fn from_seed(seed: [u8; 32]) -> Xoshiro256StarStar { deal_with_zero_seed!(seed, Self); - let mut state = [0; 4]; - read_u64_into(&seed, &mut state); - Xoshiro256StarStar { s: state } + Xoshiro256StarStar { + s: utils::read_words(&seed), + } } /// Seed a `Xoshiro256StarStar` from a `u64` using `SplitMix64`. @@ -109,7 +108,7 @@ impl RngCore for Xoshiro256StarStar { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } diff --git a/rand_xoshiro/src/xoshiro512plus.rs b/rand_xoshiro/src/xoshiro512plus.rs index 5bd276f7..915dd671 100644 --- a/rand_xoshiro/src/xoshiro512plus.rs +++ b/rand_xoshiro/src/xoshiro512plus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -92,9 +91,9 @@ impl SeedableRng for Xoshiro512Plus { #[inline] fn from_seed(seed: Seed512) -> Xoshiro512Plus { deal_with_zero_seed!(seed, Self); - let mut state = [0; 8]; - read_u64_into(&seed.0, &mut state); - Xoshiro512Plus { s: state } + Xoshiro512Plus { + s: utils::read_words(seed.as_ref()), + } } /// Seed a `Xoshiro512Plus` from a `u64` using `SplitMix64`. @@ -120,7 +119,7 @@ impl RngCore for Xoshiro512Plus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } diff --git a/rand_xoshiro/src/xoshiro512plusplus.rs b/rand_xoshiro/src/xoshiro512plusplus.rs index 82bcefc5..43e5e8e2 100644 --- a/rand_xoshiro/src/xoshiro512plusplus.rs +++ b/rand_xoshiro/src/xoshiro512plusplus.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -91,9 +90,9 @@ impl SeedableRng for Xoshiro512PlusPlus { #[inline] fn from_seed(seed: Seed512) -> Xoshiro512PlusPlus { deal_with_zero_seed!(seed, Self); - let mut state = [0; 8]; - read_u64_into(&seed.0, &mut state); - Xoshiro512PlusPlus { s: state } + Xoshiro512PlusPlus { + s: utils::read_words(seed.as_ref()), + } } /// Seed a `Xoshiro512PlusPlus` from a `u64` using `SplitMix64`. @@ -119,7 +118,7 @@ impl RngCore for Xoshiro512PlusPlus { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } } diff --git a/rand_xoshiro/src/xoshiro512starstar.rs b/rand_xoshiro/src/xoshiro512starstar.rs index c4fbefad..d7112f09 100644 --- a/rand_xoshiro/src/xoshiro512starstar.rs +++ b/rand_xoshiro/src/xoshiro512starstar.rs @@ -6,8 +6,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use rand_core::le::{fill_bytes_via_next, read_u64_into}; -use rand_core::{RngCore, SeedableRng}; +use rand_core::{RngCore, SeedableRng, utils}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; @@ -91,9 +90,9 @@ impl SeedableRng for Xoshiro512StarStar { #[inline] fn from_seed(seed: Seed512) -> Xoshiro512StarStar { deal_with_zero_seed!(seed, Self); - let mut state = [0; 8]; - read_u64_into(&seed.0, &mut state); - Xoshiro512StarStar { s: state } + Xoshiro512StarStar { + s: utils::read_words(seed.as_ref()), + } } /// Seed a `Xoshiro512StarStar` from a `u64` using `SplitMix64`. @@ -119,7 +118,7 @@ impl RngCore for Xoshiro512StarStar { #[inline] fn fill_bytes(&mut self, dest: &mut [u8]) { - fill_bytes_via_next(self, dest); + utils::fill_bytes_via_next_word(dest, || self.next_u64()); } }