Skip to content

Commit b0c08ed

Browse files
committed
rangeint: implement Hash for ranged integers manually
Only the actual value matters. By deriving `Hash`, it was including the tracked min/max values, which is of course incorrect. Note that this was only an issue in debug mode. In release mode, the min/max values aren't compiled in, and so the `Hash` impl was correct. Fixes #330
1 parent d08fcb3 commit b0c08ed

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Enhancements:
99
* [#296](https://github.com/BurntSushi/jiff/issues/296):
1010
Provide a better panic message when `Zoned::now()` fails on WASM.
1111

12+
Bug fixes:
13+
14+
* [#330](https://github.com/BurntSushi/jiff/issues/330):
15+
Fix bug where `Hash` on datetime types could yield different hash values for
16+
the same underlying date/time.
17+
1218

1319
0.2.8 (2025-04-13)
1420
==================

src/util/rangeint.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! define_ranged {
2222
smaller { $($smaller_name:ident $smaller_repr:ty),* },
2323
bigger { $($bigger_name:ident $bigger_repr:ty),* }
2424
) => {
25-
#[derive(Clone, Copy, Hash)]
25+
#[derive(Clone, Copy)]
2626
pub(crate) struct $name<const MIN: i128, const MAX: i128> {
2727
/// The actual value of the integer.
2828
///
@@ -925,6 +925,16 @@ macro_rules! define_ranged {
925925
}
926926
}
927927

928+
// We hand-write the `Hash` impl to avoid the min/max values
929+
// influencing the hash. Only the actual value should be hashed.
930+
//
931+
// See: https://github.com/BurntSushi/jiff/issues/330
932+
impl<const MIN: i128, const MAX: i128> core::hash::Hash for $name<MIN, MAX> {
933+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
934+
self.val.hash(state);
935+
}
936+
}
937+
928938
impl<
929939
const MIN1: i128,
930940
const MAX1: i128,

0 commit comments

Comments
 (0)