@@ -8,43 +8,103 @@ use std::hash::Hasher;
8
8
#[ cfg( test) ]
9
9
mod tests;
10
10
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
+ ///
16
13
/// When hashing something that ends up affecting properties like symbol names,
17
14
/// we want these symbol names to be calculated independently of other factors
18
15
/// like what architecture you're compiling *from*.
19
16
///
20
17
/// To that end we always convert integers to little-endian format before
21
18
/// hashing and the architecture dependent `isize` and `usize` types are
22
19
/// 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]
23
40
pub struct StableHasher {
24
41
state : SipHasher128 ,
25
42
}
26
43
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 ;
31
66
}
32
67
33
68
impl StableHasher {
69
+ /// Creates a new [`StableHasher`].
70
+ ///
71
+ /// To be used with the [`Hasher`] implementation and [`StableHasher::finish`].
34
72
#[ inline]
73
+ #[ must_use]
35
74
pub fn new ( ) -> Self {
36
75
StableHasher {
37
76
state : SipHasher128 :: new_with_keys ( 0 , 0 ) ,
38
77
}
39
78
}
40
79
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`].
41
86
#[ inline]
87
+ #[ must_use]
42
88
pub fn finish < W : StableHasherResult > ( self ) -> W {
43
89
W :: finish ( self . state . finish128 ( ) )
44
90
}
45
91
}
46
92
93
+ impl fmt:: Debug for StableHasher {
94
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
95
+ write ! ( f, "{:?}" , self . state)
96
+ }
97
+ }
98
+
47
99
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>
48
108
fn finish ( & self ) -> u64 {
49
109
panic ! ( "use StableHasher::finalize instead" ) ;
50
110
}
0 commit comments