Skip to content

Commit

Permalink
Merge pull request #49 from artichoke/arrat-traits-1.47-msrv
Browse files Browse the repository at this point in the history
Derive Hash, PartialEq, Eq, PartialOrd, Ord on mersenne twister RNGs
  • Loading branch information
lopopolo authored Dec 19, 2020
2 parents 78ea91f + 8e21ce5 commit eb36dd4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 68 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ jobs:
- name: Test with no default features
run: cargo test --no-default-features

build-msrv:
name: Build (1.47.0)
runs-on: ubuntu-latest
env:
RUSTFLAGS: -D warnings
RUST_BACKTRACE: 1
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: "1.47.0"
profile: minimal

- name: Compile
run: cargo build --verbose

- name: Compile tests
run: cargo test --no-run

- name: Test
run: cargo test

- name: Test with all features
run: cargo test --all-features

- name: Test with no default features
run: cargo test --no-default-features

rust:
name: Lint and format Rust
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Mersenne Twister requires ~2.5KB of internal state. To make the RNGs implemented
in this crate practical to embed in other structs, you may wish to store the RNG
in a `Box`.

### Minimum Supported Rust Version

This crate requires at least Rust 1.47.0. This version can be bumped in minor
releases.

## License

`rand_mt` is distributed under the terms of either the
Expand Down
36 changes: 2 additions & 34 deletions src/mt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use core::cmp::Ordering;
use core::convert::TryFrom;
use core::fmt;
use core::hash;
use core::num::Wrapping;

use rand_core::{RngCore, SeedableRng};
Expand Down Expand Up @@ -47,46 +45,16 @@ const LOWER_MASK: Wrapping<u32> = Wrapping(0x7fff_ffff);
/// assert_eq!(2504, mem::size_of::<Mt19937GenRand32>());
/// assert_eq!(mem::size_of::<Mt19937GenRand64>(), mem::size_of::<Mt19937GenRand32>());
/// ```
#[derive(Clone)]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::module_name_repetitions)]
pub struct Mt19937GenRand32 {
idx: usize,
state: [Wrapping<u32>; N],
}

impl Eq for Mt19937GenRand32 {}

impl PartialEq for Mt19937GenRand32 {
fn eq(&self, other: &Self) -> bool {
self.state[..] == other.state[..] && self.idx == other.idx
}
}

impl Ord for Mt19937GenRand32 {
fn cmp(&self, other: &Self) -> Ordering {
match self.state[..].cmp(&other.state[..]) {
Ordering::Equal => self.idx.cmp(&other.idx),
ordering => ordering,
}
}
}

impl PartialOrd for Mt19937GenRand32 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl hash::Hash for Mt19937GenRand32 {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.idx.hash(state);
self.state.hash(state);
}
}

impl fmt::Debug for Mt19937GenRand32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mt19937GenRand32 {{}}")
f.write_str("Mt19937GenRand64 {}")
}
}

Expand Down
36 changes: 2 additions & 34 deletions src/mt64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.

use core::cmp::Ordering;
use core::convert::TryFrom;
use core::fmt;
use core::hash;
use core::num::Wrapping;

use rand_core::{RngCore, SeedableRng};
Expand Down Expand Up @@ -45,45 +43,15 @@ const LM: Wrapping<u64> = Wrapping(0x7fff_ffff); // Least significant 31 bits
/// assert_eq!(2504, mem::size_of::<Mt19937GenRand64>());
/// assert_eq!(mem::size_of::<Mt19937GenRand32>(), mem::size_of::<Mt19937GenRand64>());
/// ```
#[derive(Clone)]
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Mt19937GenRand64 {
idx: usize,
state: [Wrapping<u64>; NN],
}

impl Eq for Mt19937GenRand64 {}

impl PartialEq for Mt19937GenRand64 {
fn eq(&self, other: &Self) -> bool {
self.state[..] == other.state[..] && self.idx == other.idx
}
}

impl Ord for Mt19937GenRand64 {
fn cmp(&self, other: &Self) -> Ordering {
match self.state[..].cmp(&other.state[..]) {
Ordering::Equal => self.idx.cmp(&other.idx),
ordering => ordering,
}
}
}

impl PartialOrd for Mt19937GenRand64 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl hash::Hash for Mt19937GenRand64 {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
self.idx.hash(state);
self.state.hash(state);
}
}

impl fmt::Debug for Mt19937GenRand64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mt19937GenRand64 {{}}")
f.write_str("Mt19937GenRand64 {}")
}
}

Expand Down

0 comments on commit eb36dd4

Please sign in to comment.