Skip to content

Commit 4b99567

Browse files
authored
impl PartialEq, Eq for rngs (#979)
Implements #974. As in #975, but defining equality such that the user is not exposed to the fact that one logical state may have different representations in an implementation-specific way.
1 parent 22cc8fe commit 4b99567

File tree

16 files changed

+56
-17
lines changed

16 files changed

+56
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ You may also find the [Upgrade Guide](https://rust-random.github.io/book/update.
1010

1111
## [Unreleased]
1212
### Additions
13+
- impl PartialEq+Eq for StdRng, SmallRng, and StepRng (#975)
1314
- Added a `serde1` feature and added Serialize/Deserialize to `UniformInt` and `WeightedIndex` (#974)
1415

1516
## [0.7.3] - 2020-01-10

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand"
3-
version = "0.7.3"
3+
version = "0.7.4"
44
authors = ["The Rand Project Developers", "The Rust Project Developers"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"
@@ -56,7 +56,7 @@ members = [
5656

5757
[dependencies]
5858
rand_core = { path = "rand_core", version = "0.5.1" }
59-
rand_pcg = { path = "rand_pcg", version = "0.2", optional = true }
59+
rand_pcg = { path = "rand_pcg", version = "0.2.1", optional = true }
6060
log = { version = "0.4.4", optional = true }
6161
serde = { version = "1.0.103", features = ["derive"], optional = true }
6262

@@ -74,9 +74,9 @@ libc = { version = "0.2.22", optional = true, default-features = false }
7474
# Emscripten does not support 128-bit integers, which are used by ChaCha code.
7575
# We work around this by using a different RNG.
7676
[target.'cfg(not(target_os = "emscripten"))'.dependencies]
77-
rand_chacha = { path = "rand_chacha", version = "0.2.1", default-features = false, optional = true }
77+
rand_chacha = { path = "rand_chacha", version = "0.2.3", default-features = false, optional = true }
7878
[target.'cfg(target_os = "emscripten")'.dependencies]
79-
rand_hc = { path = "rand_hc", version = "0.2", optional = true }
79+
rand_hc = { path = "rand_hc", version = "0.2.1", optional = true }
8080

8181
[dev-dependencies]
8282
rand_pcg = { path = "rand_pcg", version = "0.2" }

rand_chacha/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
- impl PartialEq+Eq for ChaChaXRng and ChaChaXCore (#975)
89
- Fix panic on block counter wrap that was occurring in debug builds
910

1011
## [0.2.2] - 2020-03-09

rand_chacha/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand_chacha"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
authors = ["The Rand Project Developers", "The Rust Project Developers", "The CryptoCorrosion Contributors"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"
@@ -20,9 +20,9 @@ appveyor = { repository = "rust-random/rand" }
2020

2121
[dependencies]
2222
rand_core = { path = "../rand_core", version = "0.5" }
23-
ppv-lite86 = { version = "0.2.6", default-features = false, features = ["simd"] }
23+
ppv-lite86 = { version = "0.2.8", default-features = false }
2424

2525
[features]
26-
default = ["std", "simd"]
26+
default = ["std"]
2727
std = ["ppv-lite86/std"]
2828
simd = [] # deprecated

rand_chacha/src/chacha.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<T> fmt::Debug for Array64<T> {
7070
macro_rules! chacha_impl {
7171
($ChaChaXCore:ident, $ChaChaXRng:ident, $rounds:expr, $doc:expr) => {
7272
#[doc=$doc]
73-
#[derive(Clone)]
73+
#[derive(Clone, PartialEq, Eq)]
7474
pub struct $ChaChaXCore {
7575
state: ChaCha,
7676
}
@@ -255,6 +255,14 @@ macro_rules! chacha_impl {
255255
}
256256
}
257257
}
258+
259+
impl PartialEq<$ChaChaXRng> for $ChaChaXRng {
260+
fn eq(&self, rhs: &$ChaChaXRng) -> bool {
261+
self.rng.core.state.stream64_eq(&rhs.rng.core.state)
262+
&& self.get_word_pos() == rhs.get_word_pos()
263+
}
264+
}
265+
impl Eq for $ChaChaXRng {}
258266
}
259267
}
260268

rand_chacha/src/guts.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const BUFBLOCKS: u64 = 1 << LOG2_BUFBLOCKS;
2121
pub(crate) const BUFSZ64: u64 = BLOCK64 * BUFBLOCKS;
2222
pub(crate) const BUFSZ: usize = BUFSZ64 as usize;
2323

24-
#[derive(Clone)]
24+
#[derive(Clone, PartialEq, Eq)]
2525
pub struct ChaCha {
2626
pub(crate) b: vec128_storage,
2727
pub(crate) c: vec128_storage,
@@ -91,6 +91,14 @@ impl ChaCha {
9191
pub fn get_stream_param(&self, param: u32) -> u64 {
9292
get_stream_param(self, param)
9393
}
94+
95+
/// Return whether rhs is equal in all parameters except current 64-bit position.
96+
#[inline]
97+
pub fn stream64_eq(&self, rhs: &Self) -> bool {
98+
let self_d: [u32; 4] = self.d.into();
99+
let rhs_d: [u32; 4] = rhs.d.into();
100+
self.b == rhs.b && self.c == rhs.c && self_d[3] == rhs_d[3] && self_d[2] == rhs_d[2]
101+
}
94102
}
95103

96104
#[inline(always)]

rand_hc/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
- impl PartialEq+Eq for Hc128Rng and Hc128Core (#975)
9+
710
## [0.2.0] - 2019-06-12
811
- Bump minor crate version since rand_core bump is a breaking change
912
- Switch to Edition 2018

rand_hc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rand_hc"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
authors = ["The Rand Project Developers"]
55
license = "MIT OR Apache-2.0"
66
readme = "README.md"

rand_hc/src/hc128.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ impl SeedableRng for Hc128Rng {
104104

105105
impl CryptoRng for Hc128Rng {}
106106

107+
impl PartialEq for Hc128Rng {
108+
fn eq(&self, rhs: &Self) -> bool {
109+
self.0.core == rhs.0.core && self.0.index() == rhs.0.index()
110+
}
111+
}
112+
impl Eq for Hc128Rng {}
113+
107114
/// The core of `Hc128Rng`, used with `BlockRng`.
108115
#[derive(Clone)]
109116
pub struct Hc128Core {
@@ -344,6 +351,14 @@ impl SeedableRng for Hc128Core {
344351

345352
impl CryptoRng for Hc128Core {}
346353

354+
// Custom PartialEq implementation as it can't currently be derived from an array of size 1024
355+
impl PartialEq for Hc128Core {
356+
fn eq(&self, rhs: &Self) -> bool {
357+
&self.t[..] == &rhs.t[..] && self.counter1024 == rhs.counter1024
358+
}
359+
}
360+
impl Eq for Hc128Core {}
361+
347362
#[cfg(test)]
348363
mod test {
349364
use super::Hc128Rng;

rand_pcg/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
- Derive PartialEq+Eq for Lcg64Xsh32, Lcg128Xsl64, and Mcg128Xsl64 (#975)
9+
710
## [0.2.1] - 2019-10-22
811
- Bump `bincode` version to 1.1.4 to fix minimal-dependency builds
912
- Removed unused `autocfg` build dependency.

0 commit comments

Comments
 (0)