Skip to content

Commit

Permalink
Implement Hash for all types
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrifuno committed Oct 11, 2023
1 parent 77511a1 commit 71ee39d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- `Hash` implementations for all types.

## [0.1.0] - 2023-09-12

Initial release.
Expand Down
14 changes: 13 additions & 1 deletion src/signed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! declare_signed_structs {
#[doc = concat!($SIZE)]
/// signed integer values.

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct $SelfT {
inner: $InnerT,
}
Expand All @@ -17,6 +17,18 @@ macro_rules! declare_signed_structs {
MultiplyAddShift($BaseT, $BaseT, u8),
MultiplyAddShiftNegate($BaseT, $BaseT, u8),
}

impl core::hash::Hash for $InnerT {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
match self {
$InnerT::Shift(n, _) => n.hash(state),
$InnerT::ShiftAndNegate(n, _) => n.hash(state),
$InnerT::MultiplyShift(n, _, _) => n.hash(state),
$InnerT::MultiplyAddShift(n, _, _) => n.hash(state),
$InnerT::MultiplyAddShiftNegate(n, _, _) => n.hash(state),
}
}
}
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/unsigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ macro_rules! declare_unsigned_structs {
/// Faster divisor for division and modulo operations by
#[doc = concat!($SIZE)]
/// unsigned integer values.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct $SelfT {
inner: $InnerT,
}
Expand All @@ -14,6 +14,16 @@ macro_rules! declare_unsigned_structs {
MultiplyShift($BaseT, $BaseT, u8),
MultiplyAddShift($BaseT, $BaseT, u8),
}

impl core::hash::Hash for $InnerT {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
match self {
$InnerT::Shift(n, _) => n.hash(state),
$InnerT::MultiplyShift(n, _, _) => n.hash(state),
$InnerT::MultiplyAddShift(n, _, _) => n.hash(state),
}
}
}
};
}

Expand Down

0 comments on commit 71ee39d

Please sign in to comment.