diff --git a/.github/workflows/workspace.yml b/.github/workflows/workspace.yml new file mode 100644 index 00000000..ef92b7f4 --- /dev/null +++ b/.github/workflows/workspace.yml @@ -0,0 +1,33 @@ +name: Workspace + +on: + pull_request: + paths-ignore: + - README.md + - "benches/**" + push: + branches: master + paths-ignore: + - README.md + - "benches/**" + +jobs: + clippy: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: 1.78.0 + components: clippy + - run: cargo clippy --all --all-targets -- -D warnings + + rustfmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: stable + components: rustfmt + - run: cargo fmt --all -- --check diff --git a/benches/Cargo.toml b/benches/Cargo.toml index 4214bbd5..a9909689 100644 --- a/benches/Cargo.toml +++ b/benches/Cargo.toml @@ -5,8 +5,6 @@ edition = "2021" rust-version = "1.61" publish = false -[dependencies] - [dev-dependencies] criterion = "0.5.0" criterion-cycles-per-byte = "0.6" diff --git a/rand_hc/src/hc128.rs b/rand_hc/src/hc128.rs index a166281e..3fed9484 100644 --- a/rand_hc/src/hc128.rs +++ b/rand_hc/src/hc128.rs @@ -150,40 +150,40 @@ impl BlockRngCore for Hc128Core { if self.counter1024 & 512 == 0 { // P block - results[0] = self.step_p(cc+0, cc+1, ee+13, ee+6, ee+4); - results[1] = self.step_p(cc+1, cc+2, ee+14, ee+7, ee+5); - results[2] = self.step_p(cc+2, cc+3, ee+15, ee+8, ee+6); - results[3] = self.step_p(cc+3, cc+4, cc+0, ee+9, ee+7); - results[4] = self.step_p(cc+4, cc+5, cc+1, ee+10, ee+8); - results[5] = self.step_p(cc+5, cc+6, cc+2, ee+11, ee+9); - results[6] = self.step_p(cc+6, cc+7, cc+3, ee+12, ee+10); - results[7] = self.step_p(cc+7, cc+8, cc+4, ee+13, ee+11); - results[8] = self.step_p(cc+8, cc+9, cc+5, ee+14, ee+12); - results[9] = self.step_p(cc+9, cc+10, cc+6, ee+15, ee+13); - results[10] = self.step_p(cc+10, cc+11, cc+7, cc+0, ee+14); - results[11] = self.step_p(cc+11, cc+12, cc+8, cc+1, ee+15); - results[12] = self.step_p(cc+12, cc+13, cc+9, cc+2, cc+0); - results[13] = self.step_p(cc+13, cc+14, cc+10, cc+3, cc+1); - results[14] = self.step_p(cc+14, cc+15, cc+11, cc+4, cc+2); - results[15] = self.step_p(cc+15, dd+0, cc+12, cc+5, cc+3); + results[0] = self.step_p(cc + 0, cc + 1, ee + 13, ee + 6, ee + 4); + results[1] = self.step_p(cc + 1, cc + 2, ee + 14, ee + 7, ee + 5); + results[2] = self.step_p(cc + 2, cc + 3, ee + 15, ee + 8, ee + 6); + results[3] = self.step_p(cc + 3, cc + 4, cc + 0, ee + 9, ee + 7); + results[4] = self.step_p(cc + 4, cc + 5, cc + 1, ee + 10, ee + 8); + results[5] = self.step_p(cc + 5, cc + 6, cc + 2, ee + 11, ee + 9); + results[6] = self.step_p(cc + 6, cc + 7, cc + 3, ee + 12, ee + 10); + results[7] = self.step_p(cc + 7, cc + 8, cc + 4, ee + 13, ee + 11); + results[8] = self.step_p(cc + 8, cc + 9, cc + 5, ee + 14, ee + 12); + results[9] = self.step_p(cc + 9, cc + 10, cc + 6, ee + 15, ee + 13); + results[10] = self.step_p(cc + 10, cc + 11, cc + 7, cc + 0, ee + 14); + results[11] = self.step_p(cc + 11, cc + 12, cc + 8, cc + 1, ee + 15); + results[12] = self.step_p(cc + 12, cc + 13, cc + 9, cc + 2, cc + 0); + results[13] = self.step_p(cc + 13, cc + 14, cc + 10, cc + 3, cc + 1); + results[14] = self.step_p(cc + 14, cc + 15, cc + 11, cc + 4, cc + 2); + results[15] = self.step_p(cc + 15, dd + 0, cc + 12, cc + 5, cc + 3); } else { // Q block - results[0] = self.step_q(cc+0, cc+1, ee+13, ee+6, ee+4); - results[1] = self.step_q(cc+1, cc+2, ee+14, ee+7, ee+5); - results[2] = self.step_q(cc+2, cc+3, ee+15, ee+8, ee+6); - results[3] = self.step_q(cc+3, cc+4, cc+0, ee+9, ee+7); - results[4] = self.step_q(cc+4, cc+5, cc+1, ee+10, ee+8); - results[5] = self.step_q(cc+5, cc+6, cc+2, ee+11, ee+9); - results[6] = self.step_q(cc+6, cc+7, cc+3, ee+12, ee+10); - results[7] = self.step_q(cc+7, cc+8, cc+4, ee+13, ee+11); - results[8] = self.step_q(cc+8, cc+9, cc+5, ee+14, ee+12); - results[9] = self.step_q(cc+9, cc+10, cc+6, ee+15, ee+13); - results[10] = self.step_q(cc+10, cc+11, cc+7, cc+0, ee+14); - results[11] = self.step_q(cc+11, cc+12, cc+8, cc+1, ee+15); - results[12] = self.step_q(cc+12, cc+13, cc+9, cc+2, cc+0); - results[13] = self.step_q(cc+13, cc+14, cc+10, cc+3, cc+1); - results[14] = self.step_q(cc+14, cc+15, cc+11, cc+4, cc+2); - results[15] = self.step_q(cc+15, dd+0, cc+12, cc+5, cc+3); + results[0] = self.step_q(cc + 0, cc + 1, ee + 13, ee + 6, ee + 4); + results[1] = self.step_q(cc + 1, cc + 2, ee + 14, ee + 7, ee + 5); + results[2] = self.step_q(cc + 2, cc + 3, ee + 15, ee + 8, ee + 6); + results[3] = self.step_q(cc + 3, cc + 4, cc + 0, ee + 9, ee + 7); + results[4] = self.step_q(cc + 4, cc + 5, cc + 1, ee + 10, ee + 8); + results[5] = self.step_q(cc + 5, cc + 6, cc + 2, ee + 11, ee + 9); + results[6] = self.step_q(cc + 6, cc + 7, cc + 3, ee + 12, ee + 10); + results[7] = self.step_q(cc + 7, cc + 8, cc + 4, ee + 13, ee + 11); + results[8] = self.step_q(cc + 8, cc + 9, cc + 5, ee + 14, ee + 12); + results[9] = self.step_q(cc + 9, cc + 10, cc + 6, ee + 15, ee + 13); + results[10] = self.step_q(cc + 10, cc + 11, cc + 7, cc + 0, ee + 14); + results[11] = self.step_q(cc + 11, cc + 12, cc + 8, cc + 1, ee + 15); + results[12] = self.step_q(cc + 12, cc + 13, cc + 9, cc + 2, cc + 0); + results[13] = self.step_q(cc + 13, cc + 14, cc + 10, cc + 3, cc + 1); + results[14] = self.step_q(cc + 14, cc + 15, cc + 11, cc + 4, cc + 2); + results[15] = self.step_q(cc + 15, dd + 0, cc + 12, cc + 5, cc + 3); } self.counter1024 = self.counter1024.wrapping_add(16); } @@ -197,9 +197,7 @@ impl Hc128Core { let temp0 = p[i511].rotate_right(23); let temp1 = p[i3].rotate_right(10); let temp2 = p[i10].rotate_right(8); - p[i] = p[i] - .wrapping_add(temp2) - .wrapping_add(temp0 ^ temp1); + p[i] = p[i].wrapping_add(temp2).wrapping_add(temp0 ^ temp1); let temp3 = { // The h1 function in HC-128 let a = p[i12] as u8; @@ -218,10 +216,7 @@ impl Hc128Core { let temp0 = q[i511].rotate_left(23); let temp1 = q[i3].rotate_left(10); let temp2 = q[i10].rotate_left(8); - q[i] = q - [i] - .wrapping_add(temp2) - .wrapping_add(temp0 ^ temp1); + q[i] = q[i].wrapping_add(temp2).wrapping_add(temp0 ^ temp1); let temp3 = { // The h2 function in HC-128 let a = q[i12] as u8; @@ -246,40 +241,40 @@ impl Hc128Core { if self.counter1024 < 512 { // P block - self.t[cc+0] = self.step_p(cc+0, cc+1, ee+13, ee+6, ee+4); - self.t[cc+1] = self.step_p(cc+1, cc+2, ee+14, ee+7, ee+5); - self.t[cc+2] = self.step_p(cc+2, cc+3, ee+15, ee+8, ee+6); - self.t[cc+3] = self.step_p(cc+3, cc+4, cc+0, ee+9, ee+7); - self.t[cc+4] = self.step_p(cc+4, cc+5, cc+1, ee+10, ee+8); - self.t[cc+5] = self.step_p(cc+5, cc+6, cc+2, ee+11, ee+9); - self.t[cc+6] = self.step_p(cc+6, cc+7, cc+3, ee+12, ee+10); - self.t[cc+7] = self.step_p(cc+7, cc+8, cc+4, ee+13, ee+11); - self.t[cc+8] = self.step_p(cc+8, cc+9, cc+5, ee+14, ee+12); - self.t[cc+9] = self.step_p(cc+9, cc+10, cc+6, ee+15, ee+13); - self.t[cc+10] = self.step_p(cc+10, cc+11, cc+7, cc+0, ee+14); - self.t[cc+11] = self.step_p(cc+11, cc+12, cc+8, cc+1, ee+15); - self.t[cc+12] = self.step_p(cc+12, cc+13, cc+9, cc+2, cc+0); - self.t[cc+13] = self.step_p(cc+13, cc+14, cc+10, cc+3, cc+1); - self.t[cc+14] = self.step_p(cc+14, cc+15, cc+11, cc+4, cc+2); - self.t[cc+15] = self.step_p(cc+15, dd+0, cc+12, cc+5, cc+3); + self.t[cc + 0] = self.step_p(cc + 0, cc + 1, ee + 13, ee + 6, ee + 4); + self.t[cc + 1] = self.step_p(cc + 1, cc + 2, ee + 14, ee + 7, ee + 5); + self.t[cc + 2] = self.step_p(cc + 2, cc + 3, ee + 15, ee + 8, ee + 6); + self.t[cc + 3] = self.step_p(cc + 3, cc + 4, cc + 0, ee + 9, ee + 7); + self.t[cc + 4] = self.step_p(cc + 4, cc + 5, cc + 1, ee + 10, ee + 8); + self.t[cc + 5] = self.step_p(cc + 5, cc + 6, cc + 2, ee + 11, ee + 9); + self.t[cc + 6] = self.step_p(cc + 6, cc + 7, cc + 3, ee + 12, ee + 10); + self.t[cc + 7] = self.step_p(cc + 7, cc + 8, cc + 4, ee + 13, ee + 11); + self.t[cc + 8] = self.step_p(cc + 8, cc + 9, cc + 5, ee + 14, ee + 12); + self.t[cc + 9] = self.step_p(cc + 9, cc + 10, cc + 6, ee + 15, ee + 13); + self.t[cc + 10] = self.step_p(cc + 10, cc + 11, cc + 7, cc + 0, ee + 14); + self.t[cc + 11] = self.step_p(cc + 11, cc + 12, cc + 8, cc + 1, ee + 15); + self.t[cc + 12] = self.step_p(cc + 12, cc + 13, cc + 9, cc + 2, cc + 0); + self.t[cc + 13] = self.step_p(cc + 13, cc + 14, cc + 10, cc + 3, cc + 1); + self.t[cc + 14] = self.step_p(cc + 14, cc + 15, cc + 11, cc + 4, cc + 2); + self.t[cc + 15] = self.step_p(cc + 15, dd + 0, cc + 12, cc + 5, cc + 3); } else { // Q block - self.t[cc+512+0] = self.step_q(cc+0, cc+1, ee+13, ee+6, ee+4); - self.t[cc+512+1] = self.step_q(cc+1, cc+2, ee+14, ee+7, ee+5); - self.t[cc+512+2] = self.step_q(cc+2, cc+3, ee+15, ee+8, ee+6); - self.t[cc+512+3] = self.step_q(cc+3, cc+4, cc+0, ee+9, ee+7); - self.t[cc+512+4] = self.step_q(cc+4, cc+5, cc+1, ee+10, ee+8); - self.t[cc+512+5] = self.step_q(cc+5, cc+6, cc+2, ee+11, ee+9); - self.t[cc+512+6] = self.step_q(cc+6, cc+7, cc+3, ee+12, ee+10); - self.t[cc+512+7] = self.step_q(cc+7, cc+8, cc+4, ee+13, ee+11); - self.t[cc+512+8] = self.step_q(cc+8, cc+9, cc+5, ee+14, ee+12); - self.t[cc+512+9] = self.step_q(cc+9, cc+10, cc+6, ee+15, ee+13); - self.t[cc+512+10] = self.step_q(cc+10, cc+11, cc+7, cc+0, ee+14); - self.t[cc+512+11] = self.step_q(cc+11, cc+12, cc+8, cc+1, ee+15); - self.t[cc+512+12] = self.step_q(cc+12, cc+13, cc+9, cc+2, cc+0); - self.t[cc+512+13] = self.step_q(cc+13, cc+14, cc+10, cc+3, cc+1); - self.t[cc+512+14] = self.step_q(cc+14, cc+15, cc+11, cc+4, cc+2); - self.t[cc+512+15] = self.step_q(cc+15, dd+0, cc+12, cc+5, cc+3); + self.t[cc + 512 + 0] = self.step_q(cc + 0, cc + 1, ee + 13, ee + 6, ee + 4); + self.t[cc + 512 + 1] = self.step_q(cc + 1, cc + 2, ee + 14, ee + 7, ee + 5); + self.t[cc + 512 + 2] = self.step_q(cc + 2, cc + 3, ee + 15, ee + 8, ee + 6); + self.t[cc + 512 + 3] = self.step_q(cc + 3, cc + 4, cc + 0, ee + 9, ee + 7); + self.t[cc + 512 + 4] = self.step_q(cc + 4, cc + 5, cc + 1, ee + 10, ee + 8); + self.t[cc + 512 + 5] = self.step_q(cc + 5, cc + 6, cc + 2, ee + 11, ee + 9); + self.t[cc + 512 + 6] = self.step_q(cc + 6, cc + 7, cc + 3, ee + 12, ee + 10); + self.t[cc + 512 + 7] = self.step_q(cc + 7, cc + 8, cc + 4, ee + 13, ee + 11); + self.t[cc + 512 + 8] = self.step_q(cc + 8, cc + 9, cc + 5, ee + 14, ee + 12); + self.t[cc + 512 + 9] = self.step_q(cc + 9, cc + 10, cc + 6, ee + 15, ee + 13); + self.t[cc + 512 + 10] = self.step_q(cc + 10, cc + 11, cc + 7, cc + 0, ee + 14); + self.t[cc + 512 + 11] = self.step_q(cc + 11, cc + 12, cc + 8, cc + 1, ee + 15); + self.t[cc + 512 + 12] = self.step_q(cc + 12, cc + 13, cc + 9, cc + 2, cc + 0); + self.t[cc + 512 + 13] = self.step_q(cc + 13, cc + 14, cc + 10, cc + 3, cc + 1); + self.t[cc + 512 + 14] = self.step_q(cc + 14, cc + 15, cc + 11, cc + 4, cc + 2); + self.t[cc + 512 + 15] = self.step_q(cc + 15, dd + 0, cc + 12, cc + 5, cc + 3); } self.counter1024 += 16; } @@ -299,7 +294,10 @@ impl Hc128Core { x.rotate_right(17) ^ x.rotate_right(19) ^ (x >> 10) } - let mut core = Self { t: [0u32; 1024], counter1024: 0 }; + let mut core = Self { + t: [0u32; 1024], + counter1024: 0, + }; let t = &mut core.t; // Expand the key and iv into P and Q diff --git a/rand_isaac/src/isaac.rs b/rand_isaac/src/isaac.rs index 496cd9c2..4e51e8ab 100644 --- a/rand_isaac/src/isaac.rs +++ b/rand_isaac/src/isaac.rs @@ -9,12 +9,13 @@ //! The ISAAC random number generator. -use core::{fmt, slice}; -use core::num::Wrapping as w; -#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; -use rand_core::{RngCore, SeedableRng, Error, le}; -use rand_core::block::{BlockRngCore, BlockRng}; use crate::isaac_array::IsaacArray; +use core::num::Wrapping as w; +use core::{fmt, slice}; +use rand_core::block::{BlockRng, BlockRngCore}; +use rand_core::{le, Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; #[allow(non_camel_case_types)] type w32 = w; @@ -89,7 +90,7 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; /// /// [`rand_hc`]: https://docs.rs/rand_hc #[derive(Debug, Clone)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct IsaacRng(BlockRng); impl RngCore for IsaacRng { @@ -138,9 +139,12 @@ impl SeedableRng for IsaacRng { /// The core of [`IsaacRng`], used with [`BlockRng`]. #[derive(Clone)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct IsaacCore { - #[cfg_attr(feature="serde1", serde(with="super::isaac_array::isaac_array_serde"))] + #[cfg_attr( + feature = "serde1", + serde(with = "super::isaac_array::isaac_array_serde") + )] mem: [w32; RAND_SIZE], a: w32, b: w32, @@ -157,16 +161,12 @@ impl fmt::Debug for IsaacCore { // Custom PartialEq implementation as it can't currently be derived from an array of size RAND_SIZE impl ::core::cmp::PartialEq for IsaacCore { fn eq(&self, other: &IsaacCore) -> bool { - self.mem[..] == other.mem[..] - && self.a == other.a - && self.b == other.b - && self.c == other.c + self.mem[..] == other.mem[..] && self.a == other.a && self.b == other.b && self.c == other.c } } // Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE -impl ::core::cmp::Eq for IsaacCore { -} +impl ::core::cmp::Eq for IsaacCore {} impl BlockRngCore for IsaacCore { type Item = u32; @@ -191,6 +191,7 @@ impl BlockRngCore for IsaacCore { /// from `results` in reverse. We read them in the normal direction, to /// make `fill_bytes` a memcopy. To maintain compatibility we fill in /// reverse. + #[rustfmt::skip] fn generate(&mut self, results: &mut IsaacArray) { self.c += w(1); // abbreviations @@ -199,20 +200,22 @@ impl BlockRngCore for IsaacCore { const MIDPOINT: usize = RAND_SIZE / 2; #[inline] - fn ind(mem:&[w32; RAND_SIZE], v: w32, amount: usize) -> w32 { + fn ind(mem: &[w32; RAND_SIZE], v: w32, amount: usize) -> w32 { let index = (v >> amount).0 as usize % RAND_SIZE; mem[index] } #[inline] - fn rngstep(mem: &mut [w32; RAND_SIZE], - results: &mut [u32; RAND_SIZE], - mix: w32, - a: &mut w32, - b: &mut w32, - base: usize, - m: usize, - m2: usize) { + fn rngstep( + mem: &mut [w32; RAND_SIZE], + results: &mut [u32; RAND_SIZE], + mix: w32, + a: &mut w32, + b: &mut w32, + base: usize, + m: usize, + m2: usize, + ) { let x = mem[base + m]; *a = mix + mem[base + m2]; let y = *a + *b + ind(mem, x, 2); @@ -223,7 +226,7 @@ impl BlockRngCore for IsaacCore { let mut m = 0; let mut m2 = MIDPOINT; - for i in (0..MIDPOINT/4).map(|i| i * 4) { + for i in (0..MIDPOINT / 4).map(|i| i * 4) { rngstep(&mut self.mem, results, a ^ (a << 13), &mut a, &mut b, i + 0, m, m2); rngstep(&mut self.mem, results, a ^ (a >> 6 ), &mut a, &mut b, i + 1, m, m2); rngstep(&mut self.mem, results, a ^ (a << 2 ), &mut a, &mut b, i + 2, m, m2); @@ -232,7 +235,7 @@ impl BlockRngCore for IsaacCore { m = MIDPOINT; m2 = 0; - for i in (0..MIDPOINT/4).map(|i| i * 4) { + for i in (0..MIDPOINT / 4).map(|i| i * 4) { rngstep(&mut self.mem, results, a ^ (a << 13), &mut a, &mut b, i + 0, m, m2); rngstep(&mut self.mem, results, a ^ (a >> 6 ), &mut a, &mut b, i + 1, m, m2); rngstep(&mut self.mem, results, a ^ (a << 2 ), &mut a, &mut b, i + 2, m, m2); @@ -272,6 +275,7 @@ impl IsaacCore { /// Then loops over all the elements the same way a second time." #[inline] fn init(mut mem: [w32; RAND_SIZE], rounds: u32) -> Self { + #[rustfmt::skip] fn mix(a: &mut w32, b: &mut w32, c: &mut w32, d: &mut w32, e: &mut w32, f: &mut w32, g: &mut w32, h: &mut w32) { *a ^= *b << 11; *d += *a; *b += *c; @@ -299,21 +303,35 @@ impl IsaacCore { // Normally this should do two passes, to make all of the seed effect // all of `mem` for _ in 0..rounds { - for i in (0..RAND_SIZE/8).map(|i| i * 8) { - a += mem[i ]; b += mem[i+1]; - c += mem[i+2]; d += mem[i+3]; - e += mem[i+4]; f += mem[i+5]; - g += mem[i+6]; h += mem[i+7]; - mix(&mut a, &mut b, &mut c, &mut d, - &mut e, &mut f, &mut g, &mut h); - mem[i ] = a; mem[i+1] = b; - mem[i+2] = c; mem[i+3] = d; - mem[i+4] = e; mem[i+5] = f; - mem[i+6] = g; mem[i+7] = h; + for i in (0..RAND_SIZE / 8).map(|i| i * 8) { + a += mem[i]; + b += mem[i + 1]; + c += mem[i + 2]; + d += mem[i + 3]; + e += mem[i + 4]; + f += mem[i + 5]; + g += mem[i + 6]; + h += mem[i + 7]; + mix( + &mut a, &mut b, &mut c, &mut d, &mut e, &mut f, &mut g, &mut h, + ); + mem[i] = a; + mem[i + 1] = b; + mem[i + 2] = c; + mem[i + 3] = d; + mem[i + 4] = e; + mem[i + 5] = f; + mem[i + 6] = g; + mem[i + 7] = h; } } - Self { mem, a: w(0), b: w(0), c: w(0) } + Self { + mem, + a: w(0), + b: w(0), + c: w(0), + } } } @@ -367,14 +385,16 @@ impl SeedableRng for IsaacCore { #[cfg(test)] mod test { - use rand_core::{RngCore, SeedableRng}; use super::IsaacRng; + use rand_core::{RngCore, SeedableRng}; #[test] fn test_isaac_construction() { // Test that various construction techniques produce a working RNG. - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + ]; let mut rng1 = IsaacRng::from_seed(seed); assert_eq!(rng1.next_u32(), 2869442790); @@ -384,47 +404,70 @@ mod test { #[test] fn test_isaac_true_values_32() { - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng1 = IsaacRng::from_seed(seed); let mut results = [0u32; 10]; - for i in results.iter_mut() { *i = rng1.next_u32(); } + for i in results.iter_mut() { + *i = rng1.next_u32(); + } let expected = [ - 2558573138, 873787463, 263499565, 2103644246, 3595684709, - 4203127393, 264982119, 2765226902, 2737944514, 3900253796]; + 2558573138, 873787463, 263499565, 2103644246, 3595684709, 4203127393, 264982119, + 2765226902, 2737944514, 3900253796, + ]; assert_eq!(results, expected); - let seed = [57,48,0,0, 50,9,1,0, 49,212,0,0, 148,38,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 57, 48, 0, 0, 50, 9, 1, 0, 49, 212, 0, 0, 148, 38, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng2 = IsaacRng::from_seed(seed); // skip forward to the 10000th number - for _ in 0..10000 { rng2.next_u32(); } + for _ in 0..10000 { + rng2.next_u32(); + } - for i in results.iter_mut() { *i = rng2.next_u32(); } + for i in results.iter_mut() { + *i = rng2.next_u32(); + } let expected = [ - 3676831399, 3183332890, 2834741178, 3854698763, 2717568474, - 1576568959, 3507990155, 179069555, 141456972, 2478885421]; + 3676831399, 3183332890, 2834741178, 3854698763, 2717568474, 1576568959, 3507990155, + 179069555, 141456972, 2478885421, + ]; assert_eq!(results, expected); } #[test] fn test_isaac_true_values_64() { // As above, using little-endian versions of above values - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng = IsaacRng::from_seed(seed); let mut results = [0u64; 5]; - for i in results.iter_mut() { *i = rng.next_u64(); } + for i in results.iter_mut() { + *i = rng.next_u64(); + } let expected = [ - 3752888579798383186, 9035083239252078381,18052294697452424037, - 11876559110374379111, 16751462502657800130]; + 3752888579798383186, + 9035083239252078381, + 18052294697452424037, + 11876559110374379111, + 16751462502657800130, + ]; assert_eq!(results, expected); } #[test] + #[rustfmt::skip] fn test_isaac_true_bytes() { - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng = IsaacRng::from_seed(seed); let mut results = [0u8; 32]; rng.fill_bytes(&mut results); @@ -437,6 +480,7 @@ mod test { } #[test] + #[rustfmt::skip] fn test_isaac_new_uninitialized() { // Compare the results from initializing `IsaacRng` with // `seed_from_u64(0)`, to make sure it is the same as the reference @@ -445,7 +489,9 @@ mod test { // first block. let mut rng = IsaacRng::seed_from_u64(0); let mut results = [0u32; 16]; - for i in results.iter_mut() { *i = rng.next_u32(); } + for i in results.iter_mut() { + *i = rng.next_u32(); + } let expected: [u32; 16] = [ 0x71D71FD2, 0xB54ADAE7, 0xD4788559, 0xC36129FA, 0x21DC1EA9, 0x3CB879CA, 0xD83B237F, 0xFA3CE5BD, @@ -456,8 +502,10 @@ mod test { #[test] fn test_isaac_clone() { - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng1 = IsaacRng::from_seed(seed); let mut rng2 = rng1.clone(); for _ in 0..16 { @@ -466,13 +514,15 @@ mod test { } #[test] - #[cfg(feature="serde1")] + #[cfg(feature = "serde1")] fn test_isaac_serde() { use bincode; - use std::io::{BufWriter, BufReader}; + use std::io::{BufReader, BufWriter}; - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng = IsaacRng::from_seed(seed); let buf: Vec = Vec::new(); @@ -481,9 +531,11 @@ mod test { let buf = buf.into_inner().unwrap(); let mut read = BufReader::new(&buf[..]); - let mut deserialized: IsaacRng = bincode::deserialize_from(&mut read).expect("Could not deserialize"); + let mut deserialized: IsaacRng = + bincode::deserialize_from(&mut read).expect("Could not deserialize"); - for _ in 0..300 { // more than the 256 buffered results + // more than the 256 buffered results + for _ in 0..300 { assert_eq!(rng.next_u32(), deserialized.next_u32()); } } diff --git a/rand_isaac/src/isaac64.rs b/rand_isaac/src/isaac64.rs index 69653aa4..e5511e79 100644 --- a/rand_isaac/src/isaac64.rs +++ b/rand_isaac/src/isaac64.rs @@ -9,12 +9,13 @@ //! The ISAAC-64 random number generator. -use core::{fmt, slice}; -use core::num::Wrapping as w; -#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; -use rand_core::{RngCore, SeedableRng, Error, le}; -use rand_core::block::{BlockRngCore, BlockRng64}; use crate::isaac_array::IsaacArray; +use core::num::Wrapping as w; +use core::{fmt, slice}; +use rand_core::block::{BlockRng64, BlockRngCore}; +use rand_core::{le, Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; #[allow(non_camel_case_types)] type w64 = w; @@ -80,7 +81,7 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN; /// [`rand_hc`]: https://docs.rs/rand_hc /// [`BlockRng64`]: rand_core::block::BlockRng64 #[derive(Debug, Clone)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Isaac64Rng(BlockRng64); impl RngCore for Isaac64Rng { @@ -129,9 +130,12 @@ impl SeedableRng for Isaac64Rng { /// The core of `Isaac64Rng`, used with `BlockRng`. #[derive(Clone)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Isaac64Core { - #[cfg_attr(feature="serde1", serde(with="super::isaac_array::isaac_array_serde"))] + #[cfg_attr( + feature = "serde1", + serde(with = "super::isaac_array::isaac_array_serde") + )] mem: [w64; RAND_SIZE], a: w64, b: w64, @@ -148,16 +152,12 @@ impl fmt::Debug for Isaac64Core { // Custom PartialEq implementation as it can't currently be derived from an array of size RAND_SIZE impl ::core::cmp::PartialEq for Isaac64Core { fn eq(&self, other: &Isaac64Core) -> bool { - self.mem[..] == other.mem[..] - && self.a == other.a - && self.b == other.b - && self.c == other.c + self.mem[..] == other.mem[..] && self.a == other.a && self.b == other.b && self.c == other.c } } // Custom Eq implementation as it can't currently be derived from an array of size RAND_SIZE -impl ::core::cmp::Eq for Isaac64Core { -} +impl ::core::cmp::Eq for Isaac64Core {} impl BlockRngCore for Isaac64Core { type Item = u64; @@ -182,6 +182,7 @@ impl BlockRngCore for Isaac64Core { /// from `results` in reverse. We read them in the normal direction, to /// make `fill_bytes` a memcopy. To maintain compatibility we fill in /// reverse. + #[rustfmt::skip] fn generate(&mut self, results: &mut IsaacArray) { self.c += w(1); // abbreviations @@ -190,20 +191,22 @@ impl BlockRngCore for Isaac64Core { const MIDPOINT: usize = RAND_SIZE / 2; #[inline] - fn ind(mem:&[w64; RAND_SIZE], v: w64, amount: usize) -> w64 { + fn ind(mem: &[w64; RAND_SIZE], v: w64, amount: usize) -> w64 { let index = (v >> amount).0 as usize % RAND_SIZE; mem[index] } #[inline] - fn rngstep(mem: &mut [w64; RAND_SIZE], - results: &mut [u64; RAND_SIZE], - mix: w64, - a: &mut w64, - b: &mut w64, - base: usize, - m: usize, - m2: usize) { + fn rngstep( + mem: &mut [w64; RAND_SIZE], + results: &mut [u64; RAND_SIZE], + mix: w64, + a: &mut w64, + b: &mut w64, + base: usize, + m: usize, + m2: usize, + ) { let x = mem[base + m]; *a = mix + mem[base + m2]; let y = *a + *b + ind(mem, x, 3); @@ -214,7 +217,7 @@ impl BlockRngCore for Isaac64Core { let mut m = 0; let mut m2 = MIDPOINT; - for i in (0..MIDPOINT/4).map(|i| i * 4) { + for i in (0..MIDPOINT / 4).map(|i| i * 4) { rngstep(&mut self.mem, results, !(a ^ (a << 21)), &mut a, &mut b, i + 0, m, m2); rngstep(&mut self.mem, results, a ^ (a >> 5 ), &mut a, &mut b, i + 1, m, m2); rngstep(&mut self.mem, results, a ^ (a << 12), &mut a, &mut b, i + 2, m, m2); @@ -223,7 +226,7 @@ impl BlockRngCore for Isaac64Core { m = MIDPOINT; m2 = 0; - for i in (0..MIDPOINT/4).map(|i| i * 4) { + for i in (0..MIDPOINT / 4).map(|i| i * 4) { rngstep(&mut self.mem, results, !(a ^ (a << 21)), &mut a, &mut b, i + 0, m, m2); rngstep(&mut self.mem, results, a ^ (a >> 5 ), &mut a, &mut b, i + 1, m, m2); rngstep(&mut self.mem, results, a ^ (a << 12), &mut a, &mut b, i + 2, m, m2); @@ -238,6 +241,7 @@ impl BlockRngCore for Isaac64Core { impl Isaac64Core { /// Create a new ISAAC-64 random number generator. fn init(mut mem: [w64; RAND_SIZE], rounds: u32) -> Self { + #[rustfmt::skip] fn mix(a: &mut w64, b: &mut w64, c: &mut w64, d: &mut w64, e: &mut w64, f: &mut w64, g: &mut w64, h: &mut w64) { *a -= *e; *f ^= *h >> 9; *h += *a; @@ -265,21 +269,35 @@ impl Isaac64Core { // Normally this should do two passes, to make all of the seed effect // all of `mem` for _ in 0..rounds { - for i in (0..RAND_SIZE/8).map(|i| i * 8) { - a += mem[i ]; b += mem[i+1]; - c += mem[i+2]; d += mem[i+3]; - e += mem[i+4]; f += mem[i+5]; - g += mem[i+6]; h += mem[i+7]; - mix(&mut a, &mut b, &mut c, &mut d, - &mut e, &mut f, &mut g, &mut h); - mem[i ] = a; mem[i+1] = b; - mem[i+2] = c; mem[i+3] = d; - mem[i+4] = e; mem[i+5] = f; - mem[i+6] = g; mem[i+7] = h; + for i in (0..RAND_SIZE / 8).map(|i| i * 8) { + a += mem[i]; + b += mem[i + 1]; + c += mem[i + 2]; + d += mem[i + 3]; + e += mem[i + 4]; + f += mem[i + 5]; + g += mem[i + 6]; + h += mem[i + 7]; + mix( + &mut a, &mut b, &mut c, &mut d, &mut e, &mut f, &mut g, &mut h, + ); + mem[i] = a; + mem[i + 1] = b; + mem[i + 2] = c; + mem[i + 3] = d; + mem[i + 4] = e; + mem[i + 5] = f; + mem[i + 6] = g; + mem[i + 7] = h; } } - Self { mem, a: w(0), b: w(0), c: w(0) } + Self { + mem, + a: w(0), + b: w(0), + c: w(0), + } } } @@ -328,14 +346,16 @@ impl SeedableRng for Isaac64Core { #[cfg(test)] mod test { - use rand_core::{RngCore, SeedableRng}; use super::Isaac64Rng; + use rand_core::{RngCore, SeedableRng}; #[test] fn test_isaac64_construction() { // Test that various construction techniques produce a working RNG. - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, + ]; let mut rng1 = Isaac64Rng::from_seed(seed); assert_eq!(rng1.next_u64(), 14964555543728284049); @@ -345,56 +365,82 @@ mod test { #[test] fn test_isaac64_true_values_64() { - let seed = [1,0,0,0, 0,0,0,0, 23,0,0,0, 0,0,0,0, - 200,1,0,0, 0,0,0,0, 210,30,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 200, 1, 0, 0, 0, 0, 0, 0, 210, 30, 0, + 0, 0, 0, 0, 0, + ]; let mut rng1 = Isaac64Rng::from_seed(seed); let mut results = [0u64; 10]; - for i in results.iter_mut() { *i = rng1.next_u64(); } + for i in results.iter_mut() { + *i = rng1.next_u64(); + } let expected = [ - 15071495833797886820, 7720185633435529318, - 10836773366498097981, 5414053799617603544, - 12890513357046278984, 17001051845652595546, - 9240803642279356310, 12558996012687158051, - 14673053937227185542, 1677046725350116783]; + 15071495833797886820, + 7720185633435529318, + 10836773366498097981, + 5414053799617603544, + 12890513357046278984, + 17001051845652595546, + 9240803642279356310, + 12558996012687158051, + 14673053937227185542, + 1677046725350116783, + ]; assert_eq!(results, expected); - let seed = [57,48,0,0, 0,0,0,0, 50,9,1,0, 0,0,0,0, - 49,212,0,0, 0,0,0,0, 148,38,0,0, 0,0,0,0]; + let seed = [ + 57, 48, 0, 0, 0, 0, 0, 0, 50, 9, 1, 0, 0, 0, 0, 0, 49, 212, 0, 0, 0, 0, 0, 0, 148, 38, + 0, 0, 0, 0, 0, 0, + ]; let mut rng2 = Isaac64Rng::from_seed(seed); // skip forward to the 10000th number - for _ in 0..10000 { rng2.next_u64(); } + for _ in 0..10000 { + rng2.next_u64(); + } - for i in results.iter_mut() { *i = rng2.next_u64(); } + for i in results.iter_mut() { + *i = rng2.next_u64(); + } let expected = [ - 18143823860592706164, 8491801882678285927, 2699425367717515619, - 17196852593171130876, 2606123525235546165, 15790932315217671084, - 596345674630742204, 9947027391921273664, 11788097613744130851, - 10391409374914919106]; + 18143823860592706164, + 8491801882678285927, + 2699425367717515619, + 17196852593171130876, + 2606123525235546165, + 15790932315217671084, + 596345674630742204, + 9947027391921273664, + 11788097613744130851, + 10391409374914919106, + ]; assert_eq!(results, expected); } #[test] fn test_isaac64_true_values_32() { - let seed = [1,0,0,0, 0,0,0,0, 23,0,0,0, 0,0,0,0, - 200,1,0,0, 0,0,0,0, 210,30,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 200, 1, 0, 0, 0, 0, 0, 0, 210, 30, 0, + 0, 0, 0, 0, 0, + ]; let mut rng = Isaac64Rng::from_seed(seed); let mut results = [0u32; 12]; - for i in results.iter_mut() { *i = rng.next_u32(); } + for i in results.iter_mut() { + *i = rng.next_u32(); + } // Subset of above values, as an LE u32 sequence let expected = [ - 3477963620, 3509106075, - 687845478, 1797495790, - 227048253, 2523132918, - 4044335064, 1260557630, - 4079741768, 3001306521, - 69157722, 3958365844]; + 3477963620, 3509106075, 687845478, 1797495790, 227048253, 2523132918, 4044335064, + 1260557630, 4079741768, 3001306521, 69157722, 3958365844, + ]; assert_eq!(results, expected); } #[test] fn test_isaac64_true_values_mixed() { - let seed = [1,0,0,0, 0,0,0,0, 23,0,0,0, 0,0,0,0, - 200,1,0,0, 0,0,0,0, 210,30,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 200, 1, 0, 0, 0, 0, 0, 0, 210, 30, 0, + 0, 0, 0, 0, 0, + ]; let mut rng = Isaac64Rng::from_seed(seed); // Test alternating between `next_u64` and `next_u32` works as expected. // Values are the same as `test_isaac64_true_values` and @@ -410,9 +456,12 @@ mod test { } #[test] + #[rustfmt::skip] fn test_isaac64_true_bytes() { - let seed = [1,0,0,0, 0,0,0,0, 23,0,0,0, 0,0,0,0, - 200,1,0,0, 0,0,0,0, 210,30,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 200, 1, 0, 0, 0, 0, 0, 0, 210, 30, 0, + 0, 0, 0, 0, 0, + ]; let mut rng = Isaac64Rng::from_seed(seed); let mut results = [0u8; 32]; rng.fill_bytes(&mut results); @@ -433,21 +482,36 @@ mod test { // first block. let mut rng = Isaac64Rng::seed_from_u64(0); let mut results = [0u64; 16]; - for i in results.iter_mut() { *i = rng.next_u64(); } + for i in results.iter_mut() { + *i = rng.next_u64(); + } let expected: [u64; 16] = [ - 0xF67DFBA498E4937C, 0x84A5066A9204F380, 0xFEE34BD5F5514DBB, - 0x4D1664739B8F80D6, 0x8607459AB52A14AA, 0x0E78BC5A98529E49, - 0xFE5332822AD13777, 0x556C27525E33D01A, 0x08643CA615F3149F, - 0xD0771FAF3CB04714, 0x30E86F68A37B008D, 0x3074EBC0488A3ADF, - 0x270645EA7A2790BC, 0x5601A0A8D3763C6A, 0x2F83071F53F325DD, - 0xB9090F3D42D2D2EA]; + 0xF67DFBA498E4937C, + 0x84A5066A9204F380, + 0xFEE34BD5F5514DBB, + 0x4D1664739B8F80D6, + 0x8607459AB52A14AA, + 0x0E78BC5A98529E49, + 0xFE5332822AD13777, + 0x556C27525E33D01A, + 0x08643CA615F3149F, + 0xD0771FAF3CB04714, + 0x30E86F68A37B008D, + 0x3074EBC0488A3ADF, + 0x270645EA7A2790BC, + 0x5601A0A8D3763C6A, + 0x2F83071F53F325DD, + 0xB9090F3D42D2D2EA, + ]; assert_eq!(results, expected); } #[test] fn test_isaac64_clone() { - let seed = [1,0,0,0, 0,0,0,0, 23,0,0,0, 0,0,0,0, - 200,1,0,0, 0,0,0,0, 210,30,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 200, 1, 0, 0, 0, 0, 0, 0, 210, 30, 0, + 0, 0, 0, 0, 0, + ]; let mut rng1 = Isaac64Rng::from_seed(seed); let mut rng2 = rng1.clone(); for _ in 0..16 { @@ -456,13 +520,15 @@ mod test { } #[test] - #[cfg(feature="serde1")] + #[cfg(feature = "serde1")] fn test_isaac64_serde() { use bincode; - use std::io::{BufWriter, BufReader}; + use std::io::{BufReader, BufWriter}; - let seed = [1,0,0,0, 23,0,0,0, 200,1,0,0, 210,30,0,0, - 57,48,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [ + 1, 0, 0, 0, 23, 0, 0, 0, 200, 1, 0, 0, 210, 30, 0, 0, 57, 48, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, + ]; let mut rng = Isaac64Rng::from_seed(seed); let buf: Vec = Vec::new(); @@ -471,9 +537,11 @@ mod test { let buf = buf.into_inner().unwrap(); let mut read = BufReader::new(&buf[..]); - let mut deserialized: Isaac64Rng = bincode::deserialize_from(&mut read).expect("Could not deserialize"); + let mut deserialized: Isaac64Rng = + bincode::deserialize_from(&mut read).expect("Could not deserialize"); - for _ in 0..300 { // more than the 256 buffered results + // more than the 256 buffered results + for _ in 0..300 { assert_eq!(rng.next_u64(), deserialized.next_u64()); } } diff --git a/rand_isaac/src/isaac_array.rs b/rand_isaac/src/isaac_array.rs index e06ce6de..afa5a160 100644 --- a/rand_isaac/src/isaac_array.rs +++ b/rand_isaac/src/isaac_array.rs @@ -13,21 +13,25 @@ // implement `AsRef`, `Default`, `Serialize`, `Deserialize`, or any other // traits for that matter. -#[cfg(feature="serde")] use serde::{Serialize, Deserialize}; +#[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))] +#[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] + #[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 { @@ -59,39 +63,45 @@ impl ::core::ops::DerefMut for IsaacArray { } } -impl ::core::default::Default for IsaacArray where T: Copy + Default { +impl ::core::default::Default for IsaacArray +where + T: Copy + Default, +{ fn default() -> IsaacArray { - IsaacArray { inner: [T::default(); RAND_SIZE] } + 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 { +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 { -} +impl ::core::cmp::Eq for IsaacArray where T: Eq {} - -#[cfg(feature="serde")] +#[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::{Deserialize, Deserializer, Serialize, Serializer}; - use serde::de::{Visitor,SeqAccess}; 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 + pub fn serialize(arr: &[T; RAND_SIZE], ser: S) -> Result where T: Serialize, - S: Serializer + S: Serializer, { use serde::ser::SerializeTuple; @@ -105,18 +115,18 @@ pub(super) mod isaac_array_serde { } #[inline] - pub fn deserialize<'de, T, D>(de: D) -> Result<[T;RAND_SIZE], D::Error> + pub fn deserialize<'de, T, D>(de: D) -> Result<[T; RAND_SIZE], D::Error> where - T: Deserialize<'de>+Default+Copy, + T: Deserialize<'de> + Default + Copy, D: Deserializer<'de>, { use core::marker::PhantomData; struct ArrayVisitor { _pd: PhantomData, } - impl<'de,T> Visitor<'de> for ArrayVisitor + impl<'de, T> Visitor<'de> for ArrayVisitor where - T: Deserialize<'de>+Default+Copy + T: Deserialize<'de> + Default + Copy, { type Value = [T; RAND_SIZE]; @@ -129,7 +139,7 @@ pub(super) mod isaac_array_serde { where A: SeqAccess<'de>, { - let mut out = [Default::default();RAND_SIZE]; + let mut out = [Default::default(); RAND_SIZE]; for i in 0..RAND_SIZE { match seq.next_element()? { @@ -142,6 +152,6 @@ pub(super) mod isaac_array_serde { } } - de.deserialize_tuple(RAND_SIZE, ArrayVisitor{_pd: PhantomData}) + de.deserialize_tuple(RAND_SIZE, ArrayVisitor { _pd: PhantomData }) } } diff --git a/rand_isaac/src/lib.rs b/rand_isaac/src/lib.rs index 7a7ea275..a507b453 100644 --- a/rand_isaac/src/lib.rs +++ b/rand_isaac/src/lib.rs @@ -10,20 +10,20 @@ //! //! To initialize a generator, use the [`SeedableRng`][rand_core::SeedableRng] trait. -#![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_isaac/0.3.0")] - +#![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_isaac/0.3.0" +)] #![deny(missing_docs)] #![deny(missing_debug_implementations)] #![doc(test(attr(allow(unused_variables), deny(warnings))))] #![allow( clippy::too_many_arguments, clippy::many_single_char_names, - clippy::identity_op, + clippy::identity_op )] - -#![cfg_attr(not(all(feature="serde", test)), no_std)] +#![cfg_attr(not(all(feature = "serde", test)), no_std)] pub mod isaac; pub mod isaac64; diff --git a/rand_xorshift/src/lib.rs b/rand_xorshift/src/lib.rs index 29a97df8..b240d1bc 100644 --- a/rand_xorshift/src/lib.rs +++ b/rand_xorshift/src/lib.rs @@ -20,20 +20,21 @@ //! let x = rng.next_u32(); //! ``` -#![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_xorshift/0.3.0")] - +#![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_xorshift/0.3.0" +)] #![forbid(unsafe_code)] #![deny(missing_docs)] #![deny(missing_debug_implementations)] - #![no_std] -use core::num::Wrapping as w; use core::fmt; -use rand_core::{RngCore, SeedableRng, Error, impls, le}; -#[cfg(feature="serde1")] use serde::{Serialize, Deserialize}; +use core::num::Wrapping as w; +use rand_core::{impls, le, Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// An Xorshift random number generator. /// @@ -50,7 +51,7 @@ use rand_core::{RngCore, SeedableRng, Error, impls, le}; /// ["Xorshift RNGs"](https://www.jstatsoft.org/v08/i14/paper). /// *Journal of Statistical Software*. Vol. 8 (Issue 14). #[derive(Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize,Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct XorShiftRng { x: w, y: w, diff --git a/rand_xorshift/tests/mod.rs b/rand_xorshift/tests/mod.rs index 7ecdeae3..ba57d052 100644 --- a/rand_xorshift/tests/mod.rs +++ b/rand_xorshift/tests/mod.rs @@ -4,7 +4,7 @@ use rand_xorshift::XorShiftRng; #[test] fn test_xorshift_construction() { // Test that various construction techniques produce a working RNG. - let seed = [1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16]; + let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; let mut rng1 = XorShiftRng::from_seed(seed); assert_eq!(rng1.next_u64(), 4325440999699518727); @@ -16,30 +16,42 @@ fn test_xorshift_construction() { #[test] fn test_xorshift_true_values() { - let seed = [16,15,14,13, 12,11,10,9, 8,7,6,5, 4,3,2,1]; + let seed = [16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; let mut rng = XorShiftRng::from_seed(seed); let mut results = [0u32; 9]; - for i in results.iter_mut() { *i = rng.next_u32(); } + for i in results.iter_mut() { + *i = rng.next_u32(); + } let expected: [u32; 9] = [ - 2081028795, 620940381, 269070770, 16943764, 854422573, 29242889, - 1550291885, 1227154591, 271695242]; + 2081028795, 620940381, 269070770, 16943764, 854422573, 29242889, 1550291885, 1227154591, + 271695242, + ]; assert_eq!(results, expected); let mut results = [0u64; 9]; - for i in results.iter_mut() { *i = rng.next_u64(); } + for i in results.iter_mut() { + *i = rng.next_u64(); + } let expected: [u64; 9] = [ - 9247529084182843387, 8321512596129439293, 14104136531997710878, - 6848554330849612046, 343577296533772213, 17828467390962600268, - 9847333257685787782, 7717352744383350108, 1133407547287910111]; + 9247529084182843387, + 8321512596129439293, + 14104136531997710878, + 6848554330849612046, + 343577296533772213, + 17828467390962600268, + 9847333257685787782, + 7717352744383350108, + 1133407547287910111, + ]; assert_eq!(results, expected); let mut results = [0u8; 32]; rng.fill_bytes(&mut results); - let expected = [102, 57, 212, 16, 233, 130, 49, 183, - 158, 187, 44, 203, 63, 149, 45, 17, - 117, 129, 131, 160, 70, 121, 158, 155, - 224, 209, 192, 53, 10, 62, 57, 72]; + let expected = [ + 102, 57, 212, 16, 233, 130, 49, 183, 158, 187, 44, 203, 63, 149, 45, 17, 117, 129, 131, + 160, 70, 121, 158, 155, 224, 209, 192, 53, 10, 62, 57, 72, + ]; assert_eq!(results, expected); } @@ -47,7 +59,7 @@ fn test_xorshift_true_values() { fn test_xorshift_zero_seed() { // Xorshift does not work with an all zero seed. // Assert it does not panic. - let seed = [0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0]; + let seed = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; let mut rng = XorShiftRng::from_seed(seed); let a = rng.next_u64(); let b = rng.next_u64(); @@ -57,7 +69,7 @@ fn test_xorshift_zero_seed() { #[test] fn test_xorshift_clone() { - let seed = [1,2,3,4, 5,5,7,8, 8,7,6,5, 4,3,2,1]; + let seed = [1, 2, 3, 4, 5, 5, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1]; let mut rng1 = XorShiftRng::from_seed(seed); let mut rng2 = rng1.clone(); for _ in 0..16 { @@ -65,13 +77,13 @@ fn test_xorshift_clone() { } } -#[cfg(feature="serde1")] +#[cfg(feature = "serde1")] #[test] fn test_xorshift_serde() { use bincode; - use std::io::{BufWriter, BufReader}; + use std::io::{BufReader, BufWriter}; - let seed = [1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16]; + let seed = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; let mut rng = XorShiftRng::from_seed(seed); let buf: Vec = Vec::new(); @@ -80,8 +92,8 @@ fn test_xorshift_serde() { let buf = buf.into_inner().unwrap(); let mut read = BufReader::new(&buf[..]); - let mut deserialized: XorShiftRng = bincode::deserialize_from(&mut read) - .expect("Could not deserialize"); + let mut deserialized: XorShiftRng = + bincode::deserialize_from(&mut read).expect("Could not deserialize"); for _ in 0..16 { assert_eq!(rng.next_u64(), deserialized.next_u64()); diff --git a/rand_xoshiro/src/common.rs b/rand_xoshiro/src/common.rs index e4d21c4f..5e9d8eaa 100644 --- a/rand_xoshiro/src/common.rs +++ b/rand_xoshiro/src/common.rs @@ -8,38 +8,38 @@ /// Initialize a RNG from a `u64` seed using `SplitMix64`. macro_rules! from_splitmix { - ($seed:expr) => { { + ($seed:expr) => {{ let mut rng = crate::SplitMix64::seed_from_u64($seed); Self::from_rng(&mut rng).unwrap() - } } + }}; } /// Apply the ** scrambler used by some RNGs from the xoshiro family. macro_rules! starstar_u64 { ($x:expr) => { $x.wrapping_mul(5).rotate_left(7).wrapping_mul(9) - } + }; } /// Apply the ** scrambler used by some RNGs from the xoshiro family. macro_rules! starstar_u32 { ($x:expr) => { $x.wrapping_mul(0x9E3779BB).rotate_left(5).wrapping_mul(5) - } + }; } /// Apply the ++ scrambler used by some RNGs from the xoshiro family. macro_rules! plusplus_u64 { ($x:expr, $y:expr, $rot:expr) => { $x.wrapping_add($y).rotate_left($rot).wrapping_add($x) - } + }; } /// Apply the ++ scrambler used by some RNGs from the xoshiro family. macro_rules! plusplus_u32 { ($x:expr, $y:expr) => { $x.wrapping_add($y).rotate_left(7).wrapping_add($x) - } + }; } /// Implement a jump function for an RNG from the xoshiro family. @@ -149,7 +149,7 @@ macro_rules! impl_xoroshiro_u32 { $self.s1 ^= $self.s0; $self.s0 = $self.s0.rotate_left(26) ^ $self.s1 ^ ($self.s1 << 9); $self.s1 = $self.s1.rotate_left(13); - } + }; } /// Implement the xoroshiro iteration. @@ -158,7 +158,7 @@ macro_rules! impl_xoroshiro_u64 { $self.s1 ^= $self.s0; $self.s0 = $self.s0.rotate_left(24) ^ $self.s1 ^ ($self.s1 << 16); $self.s1 = $self.s1.rotate_left(37); - } + }; } /// Implement the xoroshiro iteration for the ++ scrambler. @@ -167,7 +167,7 @@ macro_rules! impl_xoroshiro_u64_plusplus { $self.s1 ^= $self.s0; $self.s0 = $self.s0.rotate_left(49) ^ $self.s1 ^ ($self.s1 << 21); $self.s1 = $self.s1.rotate_left(28); - } + }; } /// Implement the xoshiro iteration for `u32` output. @@ -183,7 +183,7 @@ macro_rules! impl_xoshiro_u32 { $self.s[2] ^= t; $self.s[3] = $self.s[3].rotate_left(11); - } + }; } /// Implement the xoshiro iteration for `u64` output. @@ -199,7 +199,7 @@ macro_rules! impl_xoshiro_u64 { $self.s[2] ^= t; $self.s[3] = $self.s[3].rotate_left(45); - } + }; } /// Implement the large-state xoshiro iteration. @@ -219,7 +219,7 @@ macro_rules! impl_xoshiro_large { $self.s[6] ^= t; $self.s[7] = $self.s[7].rotate_left(21); - } + }; } /// Map an all-zero seed to a different one. diff --git a/rand_xoshiro/src/lib.rs b/rand_xoshiro/src/lib.rs index 4d6352f6..1ab04528 100644 --- a/rand_xoshiro/src/lib.rs +++ b/rand_xoshiro/src/lib.rs @@ -80,10 +80,11 @@ //! [xoshiro paper]: http://vigna.di.unimi.it/ftp/papers/ScrambledLinear.pdf //! [low linear complexity]: http://xoshiro.di.unimi.it/lowcomp.php -#![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.6.0")] - +#![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.6.0" +)] #![forbid(unsafe_code)] #![deny(missing_docs)] #![deny(missing_debug_implementations)] @@ -93,35 +94,35 @@ #[macro_use] mod common; mod splitmix64; -mod xoshiro128starstar; -mod xoshiro128plusplus; -mod xoshiro128plus; -mod xoshiro256starstar; -mod xoshiro256plusplus; -mod xoshiro256plus; -mod xoshiro512starstar; -mod xoshiro512plusplus; -mod xoshiro512plus; mod xoroshiro128plus; mod xoroshiro128plusplus; mod xoroshiro128starstar; -mod xoroshiro64starstar; mod xoroshiro64star; +mod xoroshiro64starstar; +mod xoshiro128plus; +mod xoshiro128plusplus; +mod xoshiro128starstar; +mod xoshiro256plus; +mod xoshiro256plusplus; +mod xoshiro256starstar; +mod xoshiro512plus; +mod xoshiro512plusplus; +mod xoshiro512starstar; +pub use common::Seed512; pub use rand_core; pub use splitmix64::SplitMix64; -pub use xoshiro128starstar::Xoshiro128StarStar; -pub use xoshiro128plusplus::Xoshiro128PlusPlus; -pub use xoshiro128plus::Xoshiro128Plus; -pub use xoshiro256starstar::Xoshiro256StarStar; -pub use xoshiro256plusplus::Xoshiro256PlusPlus; -pub use xoshiro256plus::Xoshiro256Plus; -pub use common::Seed512; -pub use xoshiro512starstar::Xoshiro512StarStar; -pub use xoshiro512plusplus::Xoshiro512PlusPlus; -pub use xoshiro512plus::Xoshiro512Plus; pub use xoroshiro128plus::Xoroshiro128Plus; -pub use xoroshiro128starstar::Xoroshiro128StarStar; pub use xoroshiro128plusplus::Xoroshiro128PlusPlus; -pub use xoroshiro64starstar::Xoroshiro64StarStar; +pub use xoroshiro128starstar::Xoroshiro128StarStar; pub use xoroshiro64star::Xoroshiro64Star; +pub use xoroshiro64starstar::Xoroshiro64StarStar; +pub use xoshiro128plus::Xoshiro128Plus; +pub use xoshiro128plusplus::Xoshiro128PlusPlus; +pub use xoshiro128starstar::Xoshiro128StarStar; +pub use xoshiro256plus::Xoshiro256Plus; +pub use xoshiro256plusplus::Xoshiro256PlusPlus; +pub use xoshiro256starstar::Xoshiro256StarStar; +pub use xoshiro512plus::Xoshiro512Plus; +pub use xoshiro512plusplus::Xoshiro512PlusPlus; +pub use xoshiro512starstar::Xoshiro512StarStar; diff --git a/rand_xoshiro/src/splitmix64.rs b/rand_xoshiro/src/splitmix64.rs index 38d6b393..2fdc9f21 100644 --- a/rand_xoshiro/src/splitmix64.rs +++ b/rand_xoshiro/src/splitmix64.rs @@ -6,10 +6,11 @@ // 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::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; -use rand_core::{RngCore, SeedableRng, Error}; +use rand_core::le::read_u64_into; +use rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A splitmix64 random number generator. /// @@ -22,7 +23,7 @@ use rand_core::{RngCore, SeedableRng, Error}; /// from [`dsiutils`](http://dsiutils.di.unimi.it/) is used. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct SplitMix64 { x: u64, } @@ -71,9 +72,7 @@ impl SeedableRng for SplitMix64 { fn from_seed(seed: [u8; 8]) -> SplitMix64 { let mut state = [0; 1]; read_u64_into(&seed, &mut state); - SplitMix64 { - x: state[0], - } + SplitMix64 { x: state[0] } } /// Seed a `SplitMix64` from a `u64`. @@ -91,24 +90,57 @@ mod tests { let mut rng = SplitMix64::seed_from_u64(1477776061723855037); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/splitmix64.c - let expected : [u64 ; 50]= [ - 1985237415132408290, 2979275885539914483, 13511426838097143398, - 8488337342461049707, 15141737807933549159, 17093170987380407015, - 16389528042912955399, 13177319091862933652, 10841969400225389492, - 17094824097954834098, 3336622647361835228, 9678412372263018368, - 11111587619974030187, 7882215801036322410, 5709234165213761869, - 7799681907651786826, 4616320717312661886, 4251077652075509767, - 7836757050122171900, 5054003328188417616, 12919285918354108358, - 16477564761813870717, 5124667218451240549, 18099554314556827626, - 7603784838804469118, 6358551455431362471, 3037176434532249502, - 3217550417701719149, 9958699920490216947, 5965803675992506258, - 12000828378049868312, 12720568162811471118, 245696019213873792, - 8351371993958923852, 14378754021282935786, 5655432093647472106, - 5508031680350692005, 8515198786865082103, 6287793597487164412, - 14963046237722101617, 3630795823534910476, 8422285279403485710, - 10554287778700714153, 10871906555720704584, 8659066966120258468, - 9420238805069527062, 10338115333623340156, 13514802760105037173, - 14635952304031724449, 15419692541594102413, + let expected: [u64; 50] = [ + 1985237415132408290, + 2979275885539914483, + 13511426838097143398, + 8488337342461049707, + 15141737807933549159, + 17093170987380407015, + 16389528042912955399, + 13177319091862933652, + 10841969400225389492, + 17094824097954834098, + 3336622647361835228, + 9678412372263018368, + 11111587619974030187, + 7882215801036322410, + 5709234165213761869, + 7799681907651786826, + 4616320717312661886, + 4251077652075509767, + 7836757050122171900, + 5054003328188417616, + 12919285918354108358, + 16477564761813870717, + 5124667218451240549, + 18099554314556827626, + 7603784838804469118, + 6358551455431362471, + 3037176434532249502, + 3217550417701719149, + 9958699920490216947, + 5965803675992506258, + 12000828378049868312, + 12720568162811471118, + 245696019213873792, + 8351371993958923852, + 14378754021282935786, + 5655432093647472106, + 5508031680350692005, + 8515198786865082103, + 6287793597487164412, + 14963046237722101617, + 3630795823534910476, + 8422285279403485710, + 10554287778700714153, + 10871906555720704584, + 8659066966120258468, + 9420238805069527062, + 10338115333623340156, + 13514802760105037173, + 14635952304031724449, + 15419692541594102413, ]; for &e in expected.iter() { assert_eq!(rng.next_u64(), e); @@ -120,27 +152,22 @@ mod tests { let mut rng = SplitMix64::seed_from_u64(10); // These values were produced with the reference implementation: // http://dsiutils.di.unimi.it/dsiutils-2.5.1-src.tar.gz - let expected : [u32 ; 100]= [ - 3930361779, 4016923089, 4113052479, 925926767, 1755287528, - 802865554, 954171070, 3724185978, 173676273, 1414488795, 12664133, - 1784889697, 1303817078, 261610523, 941280008, 2571813643, - 2954453492, 378291111, 2546873158, 3923319175, 645257028, - 3881821278, 2681538690, 3037029984, 1999958137, 1853970361, - 2989951788, 2126166628, 839962987, 3989679659, 3656977858, - 684284364, 1673258011, 170979192, 3037622326, 1600748179, - 1780764218, 1141430714, 4139736875, 3336905707, 2262051600, - 3830850262, 2430765325, 1073032139, 1668888979, 2716938970, - 4102420032, 40305196, 386350562, 2754480591, 622869439, 2129598760, - 2306038241, 4218338739, 412298926, 3453855056, 3061469690, - 4284292697, 994843708, 1591016681, 414726151, 1238182607, 18073498, - 1237631493, 351884714, 2347486264, 2488990876, 802846256, 645670443, - 957607012, 3126589776, 1966356370, 3036485766, 868696717, - 2808613630, 2070968151, 1025536863, 1743949425, 466212687, - 2994327271, 209776458, 1246125124, 3344380309, 2203947859, - 968313105, 2805485302, 197484837, 3472483632, 3931823935, - 3288490351, 4165666529, 3671080416, 689542830, 1272555356, - 1039141475, 3984640460, 4142959054, 2252788890, 2459379590, - 991872507, + let expected: [u32; 100] = [ + 3930361779, 4016923089, 4113052479, 925926767, 1755287528, 802865554, 954171070, + 3724185978, 173676273, 1414488795, 12664133, 1784889697, 1303817078, 261610523, + 941280008, 2571813643, 2954453492, 378291111, 2546873158, 3923319175, 645257028, + 3881821278, 2681538690, 3037029984, 1999958137, 1853970361, 2989951788, 2126166628, + 839962987, 3989679659, 3656977858, 684284364, 1673258011, 170979192, 3037622326, + 1600748179, 1780764218, 1141430714, 4139736875, 3336905707, 2262051600, 3830850262, + 2430765325, 1073032139, 1668888979, 2716938970, 4102420032, 40305196, 386350562, + 2754480591, 622869439, 2129598760, 2306038241, 4218338739, 412298926, 3453855056, + 3061469690, 4284292697, 994843708, 1591016681, 414726151, 1238182607, 18073498, + 1237631493, 351884714, 2347486264, 2488990876, 802846256, 645670443, 957607012, + 3126589776, 1966356370, 3036485766, 868696717, 2808613630, 2070968151, 1025536863, + 1743949425, 466212687, 2994327271, 209776458, 1246125124, 3344380309, 2203947859, + 968313105, 2805485302, 197484837, 3472483632, 3931823935, 3288490351, 4165666529, + 3671080416, 689542830, 1272555356, 1039141475, 3984640460, 4142959054, 2252788890, + 2459379590, 991872507, ]; for &e in expected.iter() { assert_eq!(rng.next_u32(), e); diff --git a/rand_xoshiro/src/xoroshiro128plus.rs b/rand_xoshiro/src/xoroshiro128plus.rs index 1639b226..73a0a047 100644 --- a/rand_xoshiro/src/xoroshiro128plus.rs +++ b/rand_xoshiro/src/xoroshiro128plus.rs @@ -6,10 +6,11 @@ // 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::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; +use rand_core::le::read_u64_into; use rand_core::{RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoroshiro128+ random number generator. /// @@ -22,7 +23,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128Plus { s0: u64, s1: u64, @@ -95,10 +96,7 @@ impl SeedableRng for Xoroshiro128Plus { let mut s = [0; 2]; read_u64_into(&seed, &mut s); - Xoroshiro128Plus { - s0: s[0], - s1: s[1], - } + Xoroshiro128Plus { s0: s[0], s1: s[1] } } /// Seed a `Xoroshiro128Plus` from a `u64` using `SplitMix64`. @@ -113,14 +111,20 @@ mod tests { #[test] fn reference() { - let mut rng = Xoroshiro128Plus::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = Xoroshiro128Plus::from_seed([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128starstar.c let expected = [ - 3, 412333834243, 2360170716294286339, 9295852285959843169, - 2797080929874688578, 6019711933173041966, 3076529664176959358, - 3521761819100106140, 7493067640054542992, 920801338098114767, + 3, + 412333834243, + 2360170716294286339, + 9295852285959843169, + 2797080929874688578, + 6019711933173041966, + 3076529664176959358, + 3521761819100106140, + 7493067640054542992, + 920801338098114767, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoroshiro128plusplus.rs b/rand_xoshiro/src/xoroshiro128plusplus.rs index 2eb04ae4..03d0ef4d 100644 --- a/rand_xoshiro/src/xoroshiro128plusplus.rs +++ b/rand_xoshiro/src/xoroshiro128plusplus.rs @@ -6,10 +6,11 @@ // 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::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; +use rand_core::le::read_u64_into; use rand_core::{RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoroshiro128++ random number generator. /// @@ -21,7 +22,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128PlusPlus { s0: u64, s1: u64, @@ -92,10 +93,7 @@ impl SeedableRng for Xoroshiro128PlusPlus { let mut s = [0; 2]; read_u64_into(&seed, &mut s); - Xoroshiro128PlusPlus { - s0: s[0], - s1: s[1], - } + Xoroshiro128PlusPlus { s0: s[0], s1: s[1] } } /// Seed a `Xoroshiro128PlusPlus` from a `u64` using `SplitMix64`. @@ -110,14 +108,21 @@ mod tests { #[test] fn reference() { - let mut rng = Xoroshiro128PlusPlus::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = + Xoroshiro128PlusPlus::from_seed([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plusplus.c let expected = [ - 393217, 669327710093319, 1732421326133921491, 11394790081659126983, - 9555452776773192676, 3586421180005889563, 1691397964866707553, - 10735626796753111697, 15216282715349408991, 14247243556711267923, + 393217, + 669327710093319, + 1732421326133921491, + 11394790081659126983, + 9555452776773192676, + 3586421180005889563, + 1691397964866707553, + 10735626796753111697, + 15216282715349408991, + 14247243556711267923, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoroshiro128starstar.rs b/rand_xoshiro/src/xoroshiro128starstar.rs index e510857c..eedae6e3 100644 --- a/rand_xoshiro/src/xoroshiro128starstar.rs +++ b/rand_xoshiro/src/xoroshiro128starstar.rs @@ -6,10 +6,11 @@ // 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::le::read_u64_into; use rand_core::impls::fill_bytes_via_next; +use rand_core::le::read_u64_into; use rand_core::{RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoroshiro128** random number generator. /// @@ -21,7 +22,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro128StarStar { s0: u64, s1: u64, @@ -92,10 +93,7 @@ impl SeedableRng for Xoroshiro128StarStar { let mut s = [0; 2]; read_u64_into(&seed, &mut s); - Xoroshiro128StarStar { - s0: s[0], - s1: s[1], - } + Xoroshiro128StarStar { s0: s[0], s1: s[1] } } /// Seed a `Xoroshiro128StarStar` from a `u64` using `SplitMix64`. @@ -110,14 +108,21 @@ mod tests { #[test] fn reference() { - let mut rng = Xoroshiro128StarStar::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = + Xoroshiro128StarStar::from_seed([1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128starstar.c let expected = [ - 5760, 97769243520, 9706862127477703552, 9223447511460779954, - 8358291023205304566, 15695619998649302768, 8517900938696309774, - 16586480348202605369, 6959129367028440372, 16822147227405758281, + 5760, + 97769243520, + 9706862127477703552, + 9223447511460779954, + 8358291023205304566, + 15695619998649302768, + 8517900938696309774, + 16586480348202605369, + 6959129367028440372, + 16822147227405758281, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoroshiro64star.rs b/rand_xoshiro/src/xoroshiro64star.rs index 4d910465..5cf557c5 100644 --- a/rand_xoshiro/src/xoroshiro64star.rs +++ b/rand_xoshiro/src/xoroshiro64star.rs @@ -6,10 +6,11 @@ // 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::le::read_u32_into; use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; +use rand_core::le::read_u32_into; use rand_core::{RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoroshiro64* random number generator. /// @@ -22,7 +23,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro64Star { s0: u32, s1: u32, @@ -63,10 +64,7 @@ impl SeedableRng for Xoroshiro64Star { let mut s = [0; 2]; read_u32_into(&seed, &mut s); - Xoroshiro64Star { - s0: s[0], - s1: s[1], - } + Xoroshiro64Star { s0: s[0], s1: s[1] } } /// Seed a `Xoroshiro64Star` from a `u64` using `SplitMix64`. @@ -85,8 +83,8 @@ mod tests { // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro64star.c let expected = [ - 2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673, - 552743735, 1672597395, 1031040050, 2755315674, + 2654435771, 327208753, 4063491769, 4259754937, 261922412, 168123673, 552743735, + 1672597395, 1031040050, 2755315674, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); diff --git a/rand_xoshiro/src/xoroshiro64starstar.rs b/rand_xoshiro/src/xoroshiro64starstar.rs index 1b0afc1a..c5e87cf0 100644 --- a/rand_xoshiro/src/xoroshiro64starstar.rs +++ b/rand_xoshiro/src/xoroshiro64starstar.rs @@ -6,10 +6,11 @@ // 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::le::read_u32_into; use rand_core::impls::{fill_bytes_via_next, next_u64_via_u32}; +use rand_core::le::read_u32_into; use rand_core::{RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoroshiro64** random number generator. /// @@ -21,7 +22,7 @@ use rand_core::{RngCore, SeedableRng}; /// David Blackman and Sebastiano Vigna. #[allow(missing_copy_implementations)] #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoroshiro64StarStar { s0: u32, s1: u32, @@ -62,10 +63,7 @@ impl SeedableRng for Xoroshiro64StarStar { let mut s = [0; 2]; read_u32_into(&seed, &mut s); - Xoroshiro64StarStar { - s0: s[0], - s1: s[1], - } + Xoroshiro64StarStar { s0: s[0], s1: s[1] } } /// Seed a `Xoroshiro64StarStar` from a `u64` using `SplitMix64`. @@ -84,8 +82,8 @@ mod tests { // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro64starstar.c let expected = [ - 3802928447, 813792938, 1618621494, 2955957307, 3252880261, - 1129983909, 2539651700, 1327610908, 1757650787, 2763843748, + 3802928447, 813792938, 1618621494, 2955957307, 3252880261, 1129983909, 2539651700, + 1327610908, 1757650787, 2763843748, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); diff --git a/rand_xoshiro/src/xoshiro128plus.rs b/rand_xoshiro/src/xoshiro128plus.rs index fb2d38e7..c5a92943 100644 --- a/rand_xoshiro/src/xoshiro128plus.rs +++ b/rand_xoshiro/src/xoshiro128plus.rs @@ -6,10 +6,11 @@ // 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::impls::{fill_bytes_via_next, next_u64_via_u32}; use rand_core::le::read_u32_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro128+ random number generator. /// @@ -21,7 +22,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, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128Plus { s: [u32; 4], } @@ -106,13 +107,12 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro128Plus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c let expected = [ - 5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097, - 4110231701, 399823256, 2144435200, + 5, 12295, 25178119, 27286542, 39879690, 1140358681, 3276312097, 4110231701, 399823256, + 2144435200, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); @@ -121,8 +121,7 @@ mod tests { #[test] fn test_jump() { - let mut rng = Xoshiro128Plus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c @@ -134,8 +133,7 @@ mod tests { #[test] fn test_long_jump() { - let mut rng = Xoshiro128Plus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = Xoshiro128Plus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.long_jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c diff --git a/rand_xoshiro/src/xoshiro128plusplus.rs b/rand_xoshiro/src/xoshiro128plusplus.rs index 3fa11712..28cb684f 100644 --- a/rand_xoshiro/src/xoshiro128plusplus.rs +++ b/rand_xoshiro/src/xoshiro128plusplus.rs @@ -6,10 +6,11 @@ // 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::impls::{fill_bytes_via_next, next_u64_via_u32}; use rand_core::le::read_u32_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro128++ random number generator. /// @@ -20,7 +21,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro128plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128PlusPlus { s: [u32; 4], } @@ -105,13 +106,13 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro128PlusPlus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128PlusPlus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plusplus.c let expected = [ - 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768, - 3867114732, 1355841295, 495546011, 621204420, + 641, 1573767, 3222811527, 3517856514, 836907274, 4247214768, 3867114732, 1355841295, + 495546011, 621204420, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); @@ -120,8 +121,8 @@ mod tests { #[test] fn test_jump() { - let mut rng = Xoshiro128PlusPlus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128PlusPlus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c @@ -133,8 +134,8 @@ mod tests { #[test] fn test_long_jump() { - let mut rng = Xoshiro128PlusPlus::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128PlusPlus::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.long_jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c diff --git a/rand_xoshiro/src/xoshiro128starstar.rs b/rand_xoshiro/src/xoshiro128starstar.rs index 449d07c6..10cc529f 100644 --- a/rand_xoshiro/src/xoshiro128starstar.rs +++ b/rand_xoshiro/src/xoshiro128starstar.rs @@ -6,10 +6,11 @@ // 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::impls::{fill_bytes_via_next, next_u64_via_u32}; use rand_core::le::read_u32_into; -use rand_core::{SeedableRng, RngCore, Error}; +use rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro128** random number generator. /// @@ -20,7 +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, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro128StarStar { s: [u32; 4], } @@ -105,13 +106,13 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro128StarStar::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); // These values were produced with the reference implementation (v1.1): // http://xoshiro.di.unimi.it/xoshiro128starstar.c let expected = [ - 11520, 0, 5927040, 70819200, 2031721883, 1637235492, 1287239034, - 3734860849, 3729100597, 4258142804, + 11520, 0, 5927040, 70819200, 2031721883, 1637235492, 1287239034, 3734860849, + 3729100597, 4258142804, ]; for &e in &expected { assert_eq!(rng.next_u32(), e); @@ -120,8 +121,8 @@ mod tests { #[test] fn test_jump() { - let mut rng = Xoshiro128StarStar::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c @@ -133,8 +134,8 @@ mod tests { #[test] fn test_long_jump() { - let mut rng = Xoshiro128StarStar::from_seed( - [1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); + let mut rng = + Xoshiro128StarStar::from_seed([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0]); rng.long_jump(); // These values were produced by instrumenting the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128plus.c diff --git a/rand_xoshiro/src/xoshiro256plus.rs b/rand_xoshiro/src/xoshiro256plus.rs index 67ce8ab8..124d1b2f 100644 --- a/rand_xoshiro/src/xoshiro256plus.rs +++ b/rand_xoshiro/src/xoshiro256plus.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro256+ random number generator. /// @@ -21,7 +22,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, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256Plus { s: [u64; 4], } @@ -43,10 +44,16 @@ impl Xoshiro256Plus { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, - 0xa9582618e03fc9aa, 0x39abdc4529b1661c - ]); + impl_jump!( + u64, + self, + [ + 0x180ec6d33cfd0aba, + 0xd5a61266f0c9392c, + 0xa9582618e03fc9aa, + 0x39abdc4529b1661c + ] + ); } /// Jump forward, equivalently to 2^192 calls to `next_u64()`. @@ -55,10 +62,16 @@ impl Xoshiro256Plus { /// `jump()` will generate 2^64 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, - 0x77710069854ee241, 0x39109bb02acbe635 - ]); + impl_jump!( + u64, + self, + [ + 0x76e15d3efefdcbbf, + 0xc5004e441c522fb3, + 0x77710069854ee241, + 0x39109bb02acbe635 + ] + ); } } @@ -114,15 +127,23 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro256Plus::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = Xoshiro256Plus::from_seed([ + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, + 0, 0, 0, + ]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro256plus.c let expected = [ - 5, 211106232532999, 211106635186183, 9223759065350669058, - 9250833439874351877, 13862484359527728515, 2346507365006083650, - 1168864526675804870, 34095955243042024, 3466914240207415127, + 5, + 211106232532999, + 211106635186183, + 9223759065350669058, + 9250833439874351877, + 13862484359527728515, + 2346507365006083650, + 1168864526675804870, + 34095955243042024, + 3466914240207415127, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoshiro256plusplus.rs b/rand_xoshiro/src/xoshiro256plusplus.rs index 57ba82e5..d9b988bf 100644 --- a/rand_xoshiro/src/xoshiro256plusplus.rs +++ b/rand_xoshiro/src/xoshiro256plusplus.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro256++ random number generator. /// @@ -20,7 +21,7 @@ use rand_core::{SeedableRng, RngCore, Error}; /// reference source code](http://xoshiro.di.unimi.it/xoshiro256plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256PlusPlus { s: [u64; 4], } @@ -42,10 +43,16 @@ impl Xoshiro256PlusPlus { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, - 0xa9582618e03fc9aa, 0x39abdc4529b1661c - ]); + impl_jump!( + u64, + self, + [ + 0x180ec6d33cfd0aba, + 0xd5a61266f0c9392c, + 0xa9582618e03fc9aa, + 0x39abdc4529b1661c + ] + ); } /// Jump forward, equivalently to 2^192 calls to `next_u64()`. @@ -54,10 +61,16 @@ impl Xoshiro256PlusPlus { /// `jump()` will generate 2^64 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, - 0x77710069854ee241, 0x39109bb02acbe635 - ]); + impl_jump!( + u64, + self, + [ + 0x76e15d3efefdcbbf, + 0xc5004e441c522fb3, + 0x77710069854ee241, + 0x39109bb02acbe635 + ] + ); } } @@ -113,15 +126,23 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro256PlusPlus::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = Xoshiro256PlusPlus::from_seed([ + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, + 0, 0, 0, + ]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro256plusplus.c let expected = [ - 41943041, 58720359, 3588806011781223, 3591011842654386, - 9228616714210784205, 9973669472204895162, 14011001112246962877, - 12406186145184390807, 15849039046786891736, 10450023813501588000, + 41943041, + 58720359, + 3588806011781223, + 3591011842654386, + 9228616714210784205, + 9973669472204895162, + 14011001112246962877, + 12406186145184390807, + 15849039046786891736, + 10450023813501588000, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoshiro256starstar.rs b/rand_xoshiro/src/xoshiro256starstar.rs index 3ee41ae4..983bda4b 100644 --- a/rand_xoshiro/src/xoshiro256starstar.rs +++ b/rand_xoshiro/src/xoshiro256starstar.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; /// A xoshiro256** random number generator. /// @@ -20,7 +21,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, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro256StarStar { s: [u64; 4], } @@ -42,10 +43,16 @@ impl Xoshiro256StarStar { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x180ec6d33cfd0aba, 0xd5a61266f0c9392c, - 0xa9582618e03fc9aa, 0x39abdc4529b1661c - ]); + impl_jump!( + u64, + self, + [ + 0x180ec6d33cfd0aba, + 0xd5a61266f0c9392c, + 0xa9582618e03fc9aa, + 0x39abdc4529b1661c + ] + ); } /// Jump forward, equivalently to 2^192 calls to `next_u64()`. @@ -54,10 +61,16 @@ impl Xoshiro256StarStar { /// `jump()` will generate 2^64 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x76e15d3efefdcbbf, 0xc5004e441c522fb3, - 0x77710069854ee241, 0x39109bb02acbe635 - ]); + impl_jump!( + u64, + self, + [ + 0x76e15d3efefdcbbf, + 0xc5004e441c522fb3, + 0x77710069854ee241, + 0x39109bb02acbe635 + ] + ); } } @@ -113,15 +126,23 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro256StarStar::from_seed( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0]); + let mut rng = Xoshiro256StarStar::from_seed([ + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, + 0, 0, 0, + ]); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro128starstar.c let expected = [ - 11520, 0, 1509978240, 1215971899390074240, 1216172134540287360, - 607988272756665600, 16172922978634559625, 8476171486693032832, - 10595114339597558777, 2904607092377533576, + 11520, + 0, + 1509978240, + 1215971899390074240, + 1216172134540287360, + 607988272756665600, + 16172922978634559625, + 8476171486693032832, + 10595114339597558777, + 2904607092377533576, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoshiro512plus.rs b/rand_xoshiro/src/xoshiro512plus.rs index e9600a32..cb83c705 100644 --- a/rand_xoshiro/src/xoshiro512plus.rs +++ b/rand_xoshiro/src/xoshiro512plus.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; use crate::Seed512; @@ -23,7 +24,7 @@ use crate::Seed512; /// reference source code](http://xoshiro.di.unimi.it/xoshiro512plus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512Plus { s: [u64; 8], } @@ -45,11 +46,20 @@ impl Xoshiro512Plus { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae, - 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c, - 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db - ]); + impl_jump!( + u64, + self, + [ + 0x33ed89b6e7a353f9, + 0x760083d7955323be, + 0x2837f2fbb5f22fae, + 0x4b8c5674d309511c, + 0xb11ac47a7ba28c25, + 0xf1be7667092bcc1c, + 0x53851efdb6df0aaf, + 0x1ebbc8b23eaf25db + ] + ); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. @@ -58,11 +68,20 @@ impl Xoshiro512Plus { /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, - 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, - 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 - ]); + impl_jump!( + u64, + self, + [ + 0x11467fef8f921d28, + 0xa2a819f2e79c8ea8, + 0xa8299fc284b3959a, + 0xb4d347340ca63ee1, + 0x1cb0940bedbff6ce, + 0xd956c5c4fa1f8e17, + 0x915e38fd4eda93bc, + 0x5b3ccdfa5d7daca5 + ] + ); } } @@ -118,6 +137,7 @@ mod tests { #[test] fn reference() { + #[rustfmt::skip] let mut rng = Xoshiro512Plus::from_seed(Seed512( [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, @@ -126,8 +146,15 @@ mod tests { // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro512plus.c let expected = [ - 4, 8, 4113, 25169936, 52776585412635, 57174648719367, - 9223482039571869716, 9331471677901559830, 9340533895746033672, + 4, + 8, + 4113, + 25169936, + 52776585412635, + 57174648719367, + 9223482039571869716, + 9331471677901559830, + 9340533895746033672, 14078399799840753678, ]; for &e in &expected { diff --git a/rand_xoshiro/src/xoshiro512plusplus.rs b/rand_xoshiro/src/xoshiro512plusplus.rs index 8d3d5269..3e5049a2 100644 --- a/rand_xoshiro/src/xoshiro512plusplus.rs +++ b/rand_xoshiro/src/xoshiro512plusplus.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; use crate::Seed512; @@ -22,7 +23,7 @@ use crate::Seed512; /// reference source code](http://xoshiro.di.unimi.it/xoshiro512plusplus.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512PlusPlus { s: [u64; 8], } @@ -44,11 +45,20 @@ impl Xoshiro512PlusPlus { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae, - 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c, - 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db - ]); + impl_jump!( + u64, + self, + [ + 0x33ed89b6e7a353f9, + 0x760083d7955323be, + 0x2837f2fbb5f22fae, + 0x4b8c5674d309511c, + 0xb11ac47a7ba28c25, + 0xf1be7667092bcc1c, + 0x53851efdb6df0aaf, + 0x1ebbc8b23eaf25db + ] + ); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. @@ -57,11 +67,20 @@ impl Xoshiro512PlusPlus { /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, - 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, - 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 - ]); + impl_jump!( + u64, + self, + [ + 0x11467fef8f921d28, + 0xa2a819f2e79c8ea8, + 0xa8299fc284b3959a, + 0xb4d347340ca63ee1, + 0x1cb0940bedbff6ce, + 0xd956c5c4fa1f8e17, + 0x915e38fd4eda93bc, + 0x5b3ccdfa5d7daca5 + ] + ); } } @@ -117,17 +136,24 @@ mod tests { #[test] fn reference() { - let mut rng = Xoshiro512PlusPlus::from_seed(Seed512( - [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, - 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, - 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0])); + let mut rng = Xoshiro512PlusPlus::from_seed(Seed512([ + 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, + 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, + 0, 0, 0, 0, 0, 0, + ])); // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro512plusplus.c let expected = [ - 524291, 1048578, 539099140, 3299073855497, 6917532603230064654, - 7494048333530275843, 14418333309547923463, 10960079161595355914, - 18279570946505382726, 10209173166699159237, + 524291, + 1048578, + 539099140, + 3299073855497, + 6917532603230064654, + 7494048333530275843, + 14418333309547923463, + 10960079161595355914, + 18279570946505382726, + 10209173166699159237, ]; for &e in &expected { assert_eq!(rng.next_u64(), e); diff --git a/rand_xoshiro/src/xoshiro512starstar.rs b/rand_xoshiro/src/xoshiro512starstar.rs index 8ec15b70..0e508602 100644 --- a/rand_xoshiro/src/xoshiro512starstar.rs +++ b/rand_xoshiro/src/xoshiro512starstar.rs @@ -6,10 +6,11 @@ // 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 rand_core::{Error, RngCore, SeedableRng}; +#[cfg(feature = "serde1")] +use serde::{Deserialize, Serialize}; use crate::Seed512; @@ -22,7 +23,7 @@ use crate::Seed512; /// reference source code](http://xoshiro.di.unimi.it/xoshiro512starstar.c) by /// David Blackman and Sebastiano Vigna. #[derive(Debug, Clone, PartialEq, Eq)] -#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] pub struct Xoshiro512StarStar { s: [u64; 8], } @@ -44,11 +45,20 @@ impl Xoshiro512StarStar { /// rng3.jump(); /// ``` pub fn jump(&mut self) { - impl_jump!(u64, self, [ - 0x33ed89b6e7a353f9, 0x760083d7955323be, 0x2837f2fbb5f22fae, - 0x4b8c5674d309511c, 0xb11ac47a7ba28c25, 0xf1be7667092bcc1c, - 0x53851efdb6df0aaf, 0x1ebbc8b23eaf25db - ]); + impl_jump!( + u64, + self, + [ + 0x33ed89b6e7a353f9, + 0x760083d7955323be, + 0x2837f2fbb5f22fae, + 0x4b8c5674d309511c, + 0xb11ac47a7ba28c25, + 0xf1be7667092bcc1c, + 0x53851efdb6df0aaf, + 0x1ebbc8b23eaf25db + ] + ); } /// Jump forward, equivalently to 2^384 calls to `next_u64()`. @@ -57,11 +67,20 @@ impl Xoshiro512StarStar { /// `jump()` will generate 2^128 non-overlapping subsequences for parallel /// distributed computations. pub fn long_jump(&mut self) { - impl_jump!(u64, self, [ - 0x11467fef8f921d28, 0xa2a819f2e79c8ea8, 0xa8299fc284b3959a, - 0xb4d347340ca63ee1, 0x1cb0940bedbff6ce, 0xd956c5c4fa1f8e17, - 0x915e38fd4eda93bc, 0x5b3ccdfa5d7daca5 - ]); + impl_jump!( + u64, + self, + [ + 0x11467fef8f921d28, + 0xa2a819f2e79c8ea8, + 0xa8299fc284b3959a, + 0xb4d347340ca63ee1, + 0x1cb0940bedbff6ce, + 0xd956c5c4fa1f8e17, + 0x915e38fd4eda93bc, + 0x5b3ccdfa5d7daca5 + ] + ); } } @@ -116,6 +135,7 @@ mod tests { use super::*; #[test] + #[rustfmt::skip] fn reference() { let mut rng = Xoshiro512StarStar::from_seed(Seed512( [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, @@ -125,8 +145,15 @@ mod tests { // These values were produced with the reference implementation: // http://xoshiro.di.unimi.it/xoshiro512starstar.c let expected = [ - 11520, 0, 23040, 23667840, 144955163520, 303992986974289920, - 25332796375735680, 296904390158016, 13911081092387501979, + 11520, + 0, + 23040, + 23667840, + 144955163520, + 303992986974289920, + 25332796375735680, + 296904390158016, + 13911081092387501979, 15304787717237593024, ]; for &e in &expected { diff --git a/rand_xoshiro/tests/serde.rs b/rand_xoshiro/tests/serde.rs index ee23a1db..3c49eb43 100644 --- a/rand_xoshiro/tests/serde.rs +++ b/rand_xoshiro/tests/serde.rs @@ -1,14 +1,16 @@ -#![cfg(feature="serde1")] +#![cfg(feature = "serde1")] use rand_core::{RngCore, SeedableRng}; -use rand_xoshiro::{SplitMix64, Xoroshiro64StarStar, Xoroshiro64Star, - Xoroshiro128Plus, Xoroshiro128StarStar, Xoshiro128StarStar, Xoshiro128Plus, - Xoshiro256StarStar, Xoshiro256Plus, Xoshiro512StarStar, Xoshiro512Plus}; +use rand_xoshiro::{ + SplitMix64, Xoroshiro128Plus, Xoroshiro128StarStar, Xoroshiro64Star, Xoroshiro64StarStar, + Xoshiro128Plus, Xoshiro128StarStar, Xoshiro256Plus, Xoshiro256StarStar, Xoshiro512Plus, + Xoshiro512StarStar, +}; macro_rules! serde_rng { ($rng:ident) => { use bincode; - use std::io::{BufWriter, BufReader}; + use std::io::{BufReader, BufWriter}; let mut rng = $rng::seed_from_u64(0); @@ -18,13 +20,13 @@ macro_rules! serde_rng { 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"); + 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]