Skip to content

Commit 7676f2d

Browse files
committed
Introduce new-type for the hash of SipHasher128(Hash)
1 parent f2780d2 commit 7676f2d

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22

3+
- Use new-type for returned-hash of `SipHasher128`(`Hash`) (#8)
34
- Introduce multi hasher support (#8)
45
- `StableHasher::finish` now returns a small hash instead of being fatal (#6)
56
- Remove `StableHasher::finalize` (#4)

src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod stable_hasher;
99
/// Hashers collection
1010
pub mod hashers {
1111
#[doc(inline)]
12-
pub use super::sip128::SipHasher128;
12+
pub use super::sip128::{SipHasher128, SipHasher128Hash};
1313

1414
/// Stable 128-bits Sip Hasher
1515
///
@@ -29,4 +29,4 @@ pub use stable_hasher::StableHasherResult;
2929
pub use stable_hasher::ExtendedHasher;
3030

3131
#[doc(inline)]
32-
pub use hashers::StableSipHasher128;
32+
pub use hashers::{SipHasher128Hash, StableSipHasher128};

src/sip128.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ const BUFFER_WITH_SPILL_SIZE: usize = BUFFER_WITH_SPILL_CAPACITY * ELEM_SIZE;
4141
// Index of the spill element in the buffer.
4242
const BUFFER_SPILL_INDEX: usize = BUFFER_WITH_SPILL_CAPACITY - 1;
4343

44+
/// Hashing result of [`SipHasher128`]
45+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
46+
pub struct SipHasher128Hash(pub [u64; 2]);
47+
4448
#[derive(Debug, Clone)]
4549
#[repr(C)]
4650
pub struct SipHasher128 {
@@ -421,7 +425,7 @@ impl Default for SipHasher128 {
421425
}
422426

423427
impl ExtendedHasher for SipHasher128 {
424-
type Hash = [u64; 2];
428+
type Hash = SipHasher128Hash;
425429

426430
#[inline]
427431
fn short_write<const LEN: usize>(&mut self, bytes: [u8; LEN]) {
@@ -446,10 +450,10 @@ impl ExtendedHasher for SipHasher128 {
446450
}
447451

448452
#[inline(always)]
449-
fn finish(mut self) -> [u64; 2] {
450-
unsafe {
453+
fn finish(mut self) -> SipHasher128Hash {
454+
SipHasher128Hash(unsafe {
451455
SipHasher128::finish128_inner(self.nbuf, &mut self.buf, self.state, self.processed)
452-
}
456+
})
453457
}
454458
}
455459

src/sip128/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ impl<'a> Hash for Bytes<'a> {
1414
}
1515
}
1616

17-
fn hash_with<T: Hash>(mut st: SipHasher128, x: &T) -> [u64; 2] {
17+
fn hash_with<T: Hash>(mut st: SipHasher128, x: &T) -> SipHasher128Hash {
1818
x.hash(&mut st);
1919
st.finish()
2020
}
2121

22-
fn hash<T: Hash>(x: &T) -> [u64; 2] {
22+
fn hash<T: Hash>(x: &T) -> SipHasher128Hash {
2323
hash_with(SipHasher128::new_with_keys(0, 0), x)
2424
}
2525

@@ -119,7 +119,7 @@ fn test_siphash_1_3_test_vector() {
119119
| ((TEST_VECTOR[i][15] as u64) << 56),
120120
];
121121

122-
assert_eq!(out, expected);
122+
assert_eq!(out.0, expected);
123123
input.push(i as u8);
124124
}
125125
}

src/stable_hasher.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,15 @@ pub trait ExtendedHasher: Hasher {
6969
/// # Example
7070
///
7171
/// ```
72-
/// use rustc_stable_hash::{StableHasher, StableHasherResult, StableSipHasher128};
72+
/// use rustc_stable_hash::hashers::{StableSipHasher128, SipHasher128Hash};
73+
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
7374
/// use std::hash::Hasher;
7475
///
7576
/// struct Hash128([u64; 2]);
7677
/// impl StableHasherResult for Hash128 {
77-
/// type Hash = [u64; 2];
78+
/// type Hash = SipHasher128Hash;
7879
///
79-
/// fn finish(hash: [u64; 2]) -> Hash128 {
80+
/// fn finish(SipHasher128Hash(hash): SipHasher128Hash) -> Hash128 {
8081
/// Hash128(hash)
8182
/// }
8283
/// }
@@ -106,7 +107,7 @@ pub struct StableHasher<H: ExtendedHasher> {
106107
/// fn finish(hash: [u64; 2]) -> Hash128 {
107108
/// let upper: u128 = hash[0] as u128;
108109
/// let lower: u128 = hash[1] as u128;
109-
///
110+
///
110111
/// Hash128((upper << 64) | lower)
111112
/// }
112113
/// }

src/stable_hasher/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::hash::Hash;
22

33
use super::*;
4-
use crate::StableSipHasher128;
4+
use crate::{SipHasher128Hash, StableSipHasher128};
55

66
// The tests below compare the computed hashes to particular expected values
77
// in order to test that we produce the same results on different platforms,
@@ -14,9 +14,9 @@ use crate::StableSipHasher128;
1414
struct TestHash([u64; 2]);
1515

1616
impl StableHasherResult for TestHash {
17-
type Hash = [u64; 2];
17+
type Hash = SipHasher128Hash;
1818

19-
fn finish(hash: Self::Hash) -> TestHash {
19+
fn finish(SipHasher128Hash(hash): Self::Hash) -> TestHash {
2020
TestHash(hash)
2121
}
2222
}

0 commit comments

Comments
 (0)