Skip to content

Commit

Permalink
fixed-hash: Derive PartialEq and Eq instead of implementing them manu…
Browse files Browse the repository at this point in the history
…ally (#742)

* derive PartialEq and Eq instead of implementing them manually

* add unit test

* cargo fmt
  • Loading branch information
fhartwig authored Apr 22, 2023
1 parent dbfbbde commit 910097e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 1 addition & 9 deletions fixed-hash/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ macro_rules! construct_fixed_hash {
( $(#[$attr:meta])* $visibility:vis struct $name:ident ( $n_bytes:expr ); ) => {
#[repr(C)]
$(#[$attr])*
#[derive(PartialEq, Eq)]
$visibility struct $name (pub [u8; $n_bytes]);

impl From<[u8; $n_bytes]> for $name {
Expand Down Expand Up @@ -263,8 +264,6 @@ macro_rules! construct_fixed_hash {
}
}

impl $crate::core_::cmp::Eq for $name {}

impl $crate::core_::cmp::PartialOrd for $name {
fn partial_cmp(&self, other: &Self) -> Option<$crate::core_::cmp::Ordering> {
Some(self.cmp(other))
Expand Down Expand Up @@ -531,13 +530,6 @@ macro_rules! impl_rand_for_fixed_hash {
#[doc(hidden)]
macro_rules! impl_cmp_for_fixed_hash {
( $name:ident ) => {
impl $crate::core_::cmp::PartialEq for $name {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_bytes() == other.as_bytes()
}
}

impl $crate::core_::cmp::Ord for $name {
#[inline]
fn cmp(&self, other: &Self) -> $crate::core_::cmp::Ordering {
Expand Down
9 changes: 9 additions & 0 deletions fixed-hash/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,15 @@ fn display_and_debug() {
test_for(0x1000, "0000000000001000", "0000…1000");
}

#[test]
fn const_matching_works() {
const ONES: H32 = H32::repeat_byte(1);
match H32::repeat_byte(0) {
ONES => unreachable!(),
_ => {},
}
}

mod ops {
use super::*;

Expand Down

0 comments on commit 910097e

Please sign in to comment.