@@ -15,9 +15,9 @@ use std::sync::Arc;
15
15
16
16
pub use strong_hash_derive:: StrongHash ;
17
17
18
- /// `StrongHasher`` is a trait for hashing any type that implements the `StrongHash`
19
- /// trait. It is similar to `std::hash::Hasher`. The key difference is that `StrongHasher`
20
- /// produces a vec of bytes as the hash.
18
+ /// `StrongHasher` is a trait for hashing functions that return more than 64
19
+ /// bits of output. The key difference between `std::hash::Hasher` and
20
+ /// `StrongHasher` is that `StrongHasher` produces a vec of bytes as the hash.
21
21
///
22
22
/// For example, a `DefaultHasher` implementation of `StrongHasher` is as follows.
23
23
///
@@ -42,13 +42,16 @@ impl<H: StrongHasher + ?Sized> StrongHasher for &mut H {
42
42
StrongHasher :: finish ( * self )
43
43
}
44
44
}
45
- /// `StrongHash`` is a trait for hashing any type that can be hashed. It is similar
46
- /// to `std::hash::Hash`` except that the hash produced is stored in as a vec of bytes
47
- /// in `Vec<u8>`` that can accommodate hashing schemes of different lengths.
48
- /// `StrongHash` is intended to be used with cryptographic hashing algorithms like blake3.
45
+
46
+ /// `StrongHash`` is a trait that is notionally similar to `std::hash::Hash`,
47
+ /// but carries the implicit expectation that the hash that will be produced
48
+ /// should be as perturbed as possible.
49
+ ///
49
50
/// When implementing `StrongHash` for an object, you should always hash the entire object
50
51
/// and not a subset or a lossy representation of the object unless you are hashing another
51
- /// cryptographic hash of the object.
52
+ /// cryptographic hash of the object. If you do the latter, a good rule of thumb
53
+ /// is that you should feel comfortable also using that same hash for equality
54
+ /// checks.
52
55
///
53
56
/// By default, `StrongHash` is implemented for most rust types that implement
54
57
/// `std::hash::Hash`. including all primitive types, strings, and vectors.
@@ -84,14 +87,14 @@ impl<H: StrongHasher + ?Sized> StrongHasher for &mut H {
84
87
/// }
85
88
/// ```
86
89
pub trait StrongHash {
87
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) ;
90
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) ;
88
91
}
89
92
90
93
macro_rules! impl_strong_hash_for_impl_hash {
91
94
( $( $t: ty) * ) => {
92
95
$(
93
96
impl StrongHash for $t {
94
- fn strong_hash<H : StrongHasher >( & self , state: & mut H ) {
97
+ fn strong_hash<H : Hasher >( & self , state: & mut H ) {
95
98
self . hash( state) ;
96
99
}
97
100
}
@@ -102,7 +105,7 @@ macro_rules! impl_strong_hash_for_impl_hash {
102
105
impl_strong_hash_for_impl_hash ! ( bool u8 i8 u16 i16 u32 i32 u64 i64 usize str & str String ) ;
103
106
104
107
impl < T : StrongHash > StrongHash for [ T ] {
105
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
108
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
106
109
self . len ( ) . strong_hash ( state) ;
107
110
for item in self . iter ( ) {
108
111
item. strong_hash ( state) ;
@@ -111,20 +114,20 @@ impl<T: StrongHash> StrongHash for [T] {
111
114
}
112
115
113
116
impl < T : StrongHash > StrongHash for & [ T ] {
114
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
117
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
115
118
<[ T ] as StrongHash >:: strong_hash ( * self , state) ;
116
119
}
117
120
}
118
121
119
122
impl < T : StrongHash > StrongHash for Vec < T > {
120
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
123
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
121
124
self . len ( ) . strong_hash ( state) ;
122
125
( & self [ ..] ) . strong_hash ( state) ;
123
126
}
124
127
}
125
128
126
129
impl < T : StrongHash > StrongHash for Option < T > {
127
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
130
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
128
131
self . is_some ( ) . strong_hash ( state) ;
129
132
if let Some ( t) = self . as_ref ( ) {
130
133
t. strong_hash ( state) ;
@@ -133,26 +136,26 @@ impl<T: StrongHash> StrongHash for Option<T> {
133
136
}
134
137
135
138
impl StrongHash for ( ) {
136
- fn strong_hash < H : StrongHasher > ( & self , _state : & mut H ) { }
139
+ fn strong_hash < H : Hasher > ( & self , _state : & mut H ) { }
137
140
}
138
141
139
142
impl < A : StrongHash > StrongHash for ( A , ) {
140
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
143
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
141
144
1 . strong_hash ( state) ;
142
145
self . 0 . strong_hash ( state) ;
143
146
}
144
147
}
145
148
146
149
impl < A : StrongHash , B : StrongHash > StrongHash for ( A , B ) {
147
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
150
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
148
151
2 . strong_hash ( state) ;
149
152
self . 0 . strong_hash ( state) ;
150
153
self . 1 . strong_hash ( state) ;
151
154
}
152
155
}
153
156
154
157
impl < A : StrongHash , B : StrongHash , C : StrongHash > StrongHash for ( A , B , C ) {
155
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
158
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
156
159
3 . strong_hash ( state) ;
157
160
self . 0 . strong_hash ( state) ;
158
161
self . 1 . strong_hash ( state) ;
@@ -161,19 +164,19 @@ impl<A: StrongHash, B: StrongHash, C: StrongHash> StrongHash for (A, B, C) {
161
164
}
162
165
163
166
impl < T : StrongHash + ?Sized > StrongHash for Box < T > {
164
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
167
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
165
168
self . as_ref ( ) . strong_hash ( state) ;
166
169
}
167
170
}
168
171
169
172
impl < T : StrongHash + ?Sized > StrongHash for Arc < T > {
170
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
173
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
171
174
self . as_ref ( ) . strong_hash ( state) ;
172
175
}
173
176
}
174
177
175
178
impl < K : StrongHash , V : StrongHash > StrongHash for BTreeMap < K , V > {
176
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
179
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
177
180
self . len ( ) . strong_hash ( state) ;
178
181
for ( k, v) in self . iter ( ) {
179
182
k. strong_hash ( state) ;
@@ -183,7 +186,7 @@ impl<K: StrongHash, V: StrongHash> StrongHash for BTreeMap<K, V> {
183
186
}
184
187
185
188
impl < K : StrongHash , V : StrongHash > StrongHash for HashMap < K , V > {
186
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
189
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
187
190
self . len ( ) . strong_hash ( state) ;
188
191
for ( k, v) in self . iter ( ) {
189
192
k. strong_hash ( state) ;
@@ -193,7 +196,7 @@ impl<K: StrongHash, V: StrongHash> StrongHash for HashMap<K, V> {
193
196
}
194
197
195
198
impl StrongHash for * const ( ) {
196
- fn strong_hash < H : StrongHasher > ( & self , state : & mut H ) {
199
+ fn strong_hash < H : Hasher > ( & self , state : & mut H ) {
197
200
( * self as usize ) . strong_hash ( state) ;
198
201
}
199
202
}
0 commit comments