Skip to content

Commit c6795f4

Browse files
authored
Merge pull request #3 from Urgau/better-doc
Improve documentation and add examples
2 parents ca68834 + 7967032 commit c6795f4

File tree

1 file changed

+69
-9
lines changed

1 file changed

+69
-9
lines changed

src/stable_hasher.rs

+69-9
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,103 @@ use std::hash::Hasher;
88
#[cfg(test)]
99
mod tests;
1010

11-
/// Trait for retrieving the result of the stable hashing operation.
12-
pub trait StableHasherResult: Sized {
13-
fn finish(hash: [u64; 2]) -> Self;
14-
}
15-
11+
/// A Stable Hasher adapted for cross-platform independent hash.
12+
///
1613
/// When hashing something that ends up affecting properties like symbol names,
1714
/// we want these symbol names to be calculated independently of other factors
1815
/// like what architecture you're compiling *from*.
1916
///
2017
/// To that end we always convert integers to little-endian format before
2118
/// hashing and the architecture dependent `isize` and `usize` types are
2219
/// extended to 64 bits if needed.
20+
///
21+
/// # Example
22+
///
23+
/// ```
24+
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
25+
/// use std::hash::Hasher;
26+
///
27+
/// struct Hash128([u64; 2]);
28+
/// impl StableHasherResult for Hash128 {
29+
/// fn finish(hash: [u64; 2]) -> Hash128 {
30+
/// Hash128(hash)
31+
/// }
32+
/// }
33+
///
34+
/// let mut hasher = StableHasher::new();
35+
/// hasher.write_usize(0xFA);
36+
///
37+
/// let hash: Hash128 = hasher.finish();
38+
/// ```
39+
#[must_use]
2340
pub struct StableHasher {
2441
state: SipHasher128,
2542
}
2643

27-
impl fmt::Debug for StableHasher {
28-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29-
write!(f, "{:?}", self.state)
30-
}
44+
/// Trait for retrieving the result of the stable hashing operation.
45+
///
46+
/// # Example
47+
///
48+
/// ```
49+
/// use rustc_stable_hash::{StableHasher, StableHasherResult};
50+
///
51+
/// struct Hash128(u128);
52+
///
53+
/// impl StableHasherResult for Hash128 {
54+
/// fn finish(hash: [u64; 2]) -> Hash128 {
55+
/// let upper: u128 = hash[0] as u128;
56+
/// let lower: u128 = hash[1] as u128;
57+
///
58+
/// Hash128((upper << 64) | lower)
59+
/// }
60+
/// }
61+
/// ```
62+
pub trait StableHasherResult: Sized {
63+
/// Retrieving the finalized state of the [`StableHasher`] and construct
64+
/// an [`Self`] containing the hash.
65+
fn finish(hasher: [u64; 2]) -> Self;
3166
}
3267

3368
impl StableHasher {
69+
/// Creates a new [`StableHasher`].
70+
///
71+
/// To be used with the [`Hasher`] implementation and [`StableHasher::finish`].
3472
#[inline]
73+
#[must_use]
3574
pub fn new() -> Self {
3675
StableHasher {
3776
state: SipHasher128::new_with_keys(0, 0),
3877
}
3978
}
4079

80+
/// Returns the typed-hash value for the values written.
81+
///
82+
/// The resulting typed-hash value is constructed from an
83+
/// [`StableHasherResult`] implemenation.
84+
///
85+
/// To be used in-place of [`Hasher::finish`].
4186
#[inline]
87+
#[must_use]
4288
pub fn finish<W: StableHasherResult>(self) -> W {
4389
W::finish(self.state.finish128())
4490
}
4591
}
4692

93+
impl fmt::Debug for StableHasher {
94+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95+
write!(f, "{:?}", self.state)
96+
}
97+
}
98+
4799
impl Hasher for StableHasher {
100+
/// <div class="warning">
101+
///
102+
/// Do not use this function, it will unconditionnaly panic.
103+
///
104+
/// Use instead [`StableHasher::finish`] which returns a
105+
/// `[u64; 2]` for greater precision.
106+
///
107+
/// </div>
48108
fn finish(&self) -> u64 {
49109
panic!("use StableHasher::finalize instead");
50110
}

0 commit comments

Comments
 (0)