1 use crate::sip128::SipHasher128;
2 use rustc_index::bit_set;
3 use rustc_index::vec;
4 use smallvec::SmallVec;
5 use std::hash::{BuildHasher, Hash, Hasher};
6 use std::mem;
7 
8 #[cfg(test)]
9 mod tests;
10 
11 /// When hashing something that ends up affecting properties like symbol names,
12 /// we want these symbol names to be calculated independently of other factors
13 /// like what architecture you're compiling *from*.
14 ///
15 /// To that end we always convert integers to little-endian format before
16 /// hashing and the architecture dependent `isize` and `usize` types are
17 /// extended to 64 bits if needed.
18 pub struct StableHasher {
19     state: SipHasher128,
20 }
21 
22 impl ::std::fmt::Debug for StableHasher {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result23     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24         write!(f, "{:?}", self.state)
25     }
26 }
27 
28 pub trait StableHasherResult: Sized {
finish(hasher: StableHasher) -> Self29     fn finish(hasher: StableHasher) -> Self;
30 }
31 
32 impl StableHasher {
33     #[inline]
new() -> Self34     pub fn new() -> Self {
35         StableHasher { state: SipHasher128::new_with_keys(0, 0) }
36     }
37 
38     #[inline]
finish<W: StableHasherResult>(self) -> W39     pub fn finish<W: StableHasherResult>(self) -> W {
40         W::finish(self)
41     }
42 }
43 
44 impl StableHasherResult for u128 {
finish(hasher: StableHasher) -> Self45     fn finish(hasher: StableHasher) -> Self {
46         let (_0, _1) = hasher.finalize();
47         u128::from(_0) | (u128::from(_1) << 64)
48     }
49 }
50 
51 impl StableHasherResult for u64 {
finish(hasher: StableHasher) -> Self52     fn finish(hasher: StableHasher) -> Self {
53         hasher.finalize().0
54     }
55 }
56 
57 impl StableHasher {
58     #[inline]
finalize(self) -> (u64, u64)59     pub fn finalize(self) -> (u64, u64) {
60         self.state.finish128()
61     }
62 }
63 
64 impl Hasher for StableHasher {
finish(&self) -> u6465     fn finish(&self) -> u64 {
66         panic!("use StableHasher::finalize instead");
67     }
68 
69     #[inline]
write(&mut self, bytes: &[u8])70     fn write(&mut self, bytes: &[u8]) {
71         self.state.write(bytes);
72     }
73 
74     #[inline]
write_u8(&mut self, i: u8)75     fn write_u8(&mut self, i: u8) {
76         self.state.write_u8(i);
77     }
78 
79     #[inline]
write_u16(&mut self, i: u16)80     fn write_u16(&mut self, i: u16) {
81         self.state.write_u16(i.to_le());
82     }
83 
84     #[inline]
write_u32(&mut self, i: u32)85     fn write_u32(&mut self, i: u32) {
86         self.state.write_u32(i.to_le());
87     }
88 
89     #[inline]
write_u64(&mut self, i: u64)90     fn write_u64(&mut self, i: u64) {
91         self.state.write_u64(i.to_le());
92     }
93 
94     #[inline]
write_u128(&mut self, i: u128)95     fn write_u128(&mut self, i: u128) {
96         self.state.write_u128(i.to_le());
97     }
98 
99     #[inline]
write_usize(&mut self, i: usize)100     fn write_usize(&mut self, i: usize) {
101         // Always treat usize as u64 so we get the same results on 32 and 64 bit
102         // platforms. This is important for symbol hashes when cross compiling,
103         // for example.
104         self.state.write_u64((i as u64).to_le());
105     }
106 
107     #[inline]
write_i8(&mut self, i: i8)108     fn write_i8(&mut self, i: i8) {
109         self.state.write_i8(i);
110     }
111 
112     #[inline]
write_i16(&mut self, i: i16)113     fn write_i16(&mut self, i: i16) {
114         self.state.write_i16(i.to_le());
115     }
116 
117     #[inline]
write_i32(&mut self, i: i32)118     fn write_i32(&mut self, i: i32) {
119         self.state.write_i32(i.to_le());
120     }
121 
122     #[inline]
write_i64(&mut self, i: i64)123     fn write_i64(&mut self, i: i64) {
124         self.state.write_i64(i.to_le());
125     }
126 
127     #[inline]
write_i128(&mut self, i: i128)128     fn write_i128(&mut self, i: i128) {
129         self.state.write_i128(i.to_le());
130     }
131 
132     #[inline]
write_isize(&mut self, i: isize)133     fn write_isize(&mut self, i: isize) {
134         // Always treat isize as i64 so we get the same results on 32 and 64 bit
135         // platforms. This is important for symbol hashes when cross compiling,
136         // for example. Sign extending here is preferable as it means that the
137         // same negative number hashes the same on both 32 and 64 bit platforms.
138         self.state.write_i64((i as i64).to_le());
139     }
140 }
141 
142 /// Something that implements `HashStable<CTX>` can be hashed in a way that is
143 /// stable across multiple compilation sessions.
144 ///
145 /// Note that `HashStable` imposes rather more strict requirements than usual
146 /// hash functions:
147 ///
148 /// - Stable hashes are sometimes used as identifiers. Therefore they must
149 ///   conform to the corresponding `PartialEq` implementations:
150 ///
151 ///     - `x == y` implies `hash_stable(x) == hash_stable(y)`, and
152 ///     - `x != y` implies `hash_stable(x) != hash_stable(y)`.
153 ///
154 ///   That second condition is usually not required for hash functions
155 ///   (e.g. `Hash`). In practice this means that `hash_stable` must feed any
156 ///   information into the hasher that a `PartialEq` comparison takes into
157 ///   account. See [#49300](https://github.com/rust-lang/rust/issues/49300)
158 ///   for an example where violating this invariant has caused trouble in the
159 ///   past.
160 ///
161 /// - `hash_stable()` must be independent of the current
162 ///    compilation session. E.g. they must not hash memory addresses or other
163 ///    things that are "randomly" assigned per compilation session.
164 ///
165 /// - `hash_stable()` must be independent of the host architecture. The
166 ///   `StableHasher` takes care of endianness and `isize`/`usize` platform
167 ///   differences.
168 pub trait HashStable<CTX> {
hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher)169     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher);
170 }
171 
172 /// Implement this for types that can be turned into stable keys like, for
173 /// example, for DefId that can be converted to a DefPathHash. This is used for
174 /// bringing maps into a predictable order before hashing them.
175 pub trait ToStableHashKey<HCX> {
176     type KeyType: Ord + Sized + HashStable<HCX>;
to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType177     fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType;
178 }
179 
180 // Implement HashStable by just calling `Hash::hash()`. This works fine for
181 // self-contained values that don't depend on the hashing context `CTX`.
182 #[macro_export]
183 macro_rules! impl_stable_hash_via_hash {
184     ($t:ty) => {
185         impl<CTX> $crate::stable_hasher::HashStable<CTX> for $t {
186             #[inline]
187             fn hash_stable(&self, _: &mut CTX, hasher: &mut $crate::stable_hasher::StableHasher) {
188                 ::std::hash::Hash::hash(self, hasher);
189             }
190         }
191     };
192 }
193 
194 impl_stable_hash_via_hash!(i8);
195 impl_stable_hash_via_hash!(i16);
196 impl_stable_hash_via_hash!(i32);
197 impl_stable_hash_via_hash!(i64);
198 impl_stable_hash_via_hash!(isize);
199 
200 impl_stable_hash_via_hash!(u8);
201 impl_stable_hash_via_hash!(u16);
202 impl_stable_hash_via_hash!(u32);
203 impl_stable_hash_via_hash!(u64);
204 impl_stable_hash_via_hash!(usize);
205 
206 impl_stable_hash_via_hash!(u128);
207 impl_stable_hash_via_hash!(i128);
208 
209 impl_stable_hash_via_hash!(char);
210 impl_stable_hash_via_hash!(());
211 
212 impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)213     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
214         self.get().hash_stable(ctx, hasher)
215     }
216 }
217 
218 impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)219     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
220         self.get().hash_stable(ctx, hasher)
221     }
222 }
223 
224 impl<CTX> HashStable<CTX> for f32 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)225     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
226         let val: u32 = unsafe { ::std::mem::transmute(*self) };
227         val.hash_stable(ctx, hasher);
228     }
229 }
230 
231 impl<CTX> HashStable<CTX> for f64 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)232     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
233         let val: u64 = unsafe { ::std::mem::transmute(*self) };
234         val.hash_stable(ctx, hasher);
235     }
236 }
237 
238 impl<CTX> HashStable<CTX> for ::std::cmp::Ordering {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)239     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
240         (*self as i8).hash_stable(ctx, hasher);
241     }
242 }
243 
244 impl<T1: HashStable<CTX>, CTX> HashStable<CTX> for (T1,) {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)245     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
246         let (ref _0,) = *self;
247         _0.hash_stable(ctx, hasher);
248     }
249 }
250 
251 impl<T1: HashStable<CTX>, T2: HashStable<CTX>, CTX> HashStable<CTX> for (T1, T2) {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)252     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
253         let (ref _0, ref _1) = *self;
254         _0.hash_stable(ctx, hasher);
255         _1.hash_stable(ctx, hasher);
256     }
257 }
258 
259 impl<T1, T2, T3, CTX> HashStable<CTX> for (T1, T2, T3)
260 where
261     T1: HashStable<CTX>,
262     T2: HashStable<CTX>,
263     T3: HashStable<CTX>,
264 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)265     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
266         let (ref _0, ref _1, ref _2) = *self;
267         _0.hash_stable(ctx, hasher);
268         _1.hash_stable(ctx, hasher);
269         _2.hash_stable(ctx, hasher);
270     }
271 }
272 
273 impl<T1, T2, T3, T4, CTX> HashStable<CTX> for (T1, T2, T3, T4)
274 where
275     T1: HashStable<CTX>,
276     T2: HashStable<CTX>,
277     T3: HashStable<CTX>,
278     T4: HashStable<CTX>,
279 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)280     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
281         let (ref _0, ref _1, ref _2, ref _3) = *self;
282         _0.hash_stable(ctx, hasher);
283         _1.hash_stable(ctx, hasher);
284         _2.hash_stable(ctx, hasher);
285         _3.hash_stable(ctx, hasher);
286     }
287 }
288 
289 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for [T] {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)290     default fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
291         self.len().hash_stable(ctx, hasher);
292         for item in self {
293             item.hash_stable(ctx, hasher);
294         }
295     }
296 }
297 
298 impl<T: HashStable<CTX>, CTX> HashStable<CTX> for Vec<T> {
299     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)300     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
301         (&self[..]).hash_stable(ctx, hasher);
302     }
303 }
304 
305 impl<K, V, R, CTX> HashStable<CTX> for indexmap::IndexMap<K, V, R>
306 where
307     K: HashStable<CTX> + Eq + Hash,
308     V: HashStable<CTX>,
309     R: BuildHasher,
310 {
311     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)312     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
313         self.len().hash_stable(ctx, hasher);
314         for kv in self {
315             kv.hash_stable(ctx, hasher);
316         }
317     }
318 }
319 
320 impl<K, R, CTX> HashStable<CTX> for indexmap::IndexSet<K, R>
321 where
322     K: HashStable<CTX> + Eq + Hash,
323     R: BuildHasher,
324 {
325     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)326     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
327         self.len().hash_stable(ctx, hasher);
328         for key in self {
329             key.hash_stable(ctx, hasher);
330         }
331     }
332 }
333 
334 impl<A, CTX> HashStable<CTX> for SmallVec<[A; 1]>
335 where
336     A: HashStable<CTX>,
337 {
338     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)339     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
340         (&self[..]).hash_stable(ctx, hasher);
341     }
342 }
343 
344 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for Box<T> {
345     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)346     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
347         (**self).hash_stable(ctx, hasher);
348     }
349 }
350 
351 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::rc::Rc<T> {
352     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)353     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
354         (**self).hash_stable(ctx, hasher);
355     }
356 }
357 
358 impl<T: ?Sized + HashStable<CTX>, CTX> HashStable<CTX> for ::std::sync::Arc<T> {
359     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)360     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
361         (**self).hash_stable(ctx, hasher);
362     }
363 }
364 
365 impl<CTX> HashStable<CTX> for str {
366     #[inline]
hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher)367     fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) {
368         self.len().hash(hasher);
369         self.as_bytes().hash(hasher);
370     }
371 }
372 
373 impl<CTX> HashStable<CTX> for String {
374     #[inline]
hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher)375     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
376         (&self[..]).hash_stable(hcx, hasher);
377     }
378 }
379 
380 impl<HCX> ToStableHashKey<HCX> for String {
381     type KeyType = String;
382     #[inline]
to_stable_hash_key(&self, _: &HCX) -> Self::KeyType383     fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
384         self.clone()
385     }
386 }
387 
388 impl<CTX> HashStable<CTX> for bool {
389     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)390     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
391         (if *self { 1u8 } else { 0u8 }).hash_stable(ctx, hasher);
392     }
393 }
394 
395 impl<T, CTX> HashStable<CTX> for Option<T>
396 where
397     T: HashStable<CTX>,
398 {
399     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)400     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
401         if let Some(ref value) = *self {
402             1u8.hash_stable(ctx, hasher);
403             value.hash_stable(ctx, hasher);
404         } else {
405             0u8.hash_stable(ctx, hasher);
406         }
407     }
408 }
409 
410 impl<T1, T2, CTX> HashStable<CTX> for Result<T1, T2>
411 where
412     T1: HashStable<CTX>,
413     T2: HashStable<CTX>,
414 {
415     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)416     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
417         mem::discriminant(self).hash_stable(ctx, hasher);
418         match *self {
419             Ok(ref x) => x.hash_stable(ctx, hasher),
420             Err(ref x) => x.hash_stable(ctx, hasher),
421         }
422     }
423 }
424 
425 impl<'a, T, CTX> HashStable<CTX> for &'a T
426 where
427     T: HashStable<CTX> + ?Sized,
428 {
429     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)430     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
431         (**self).hash_stable(ctx, hasher);
432     }
433 }
434 
435 impl<T, CTX> HashStable<CTX> for ::std::mem::Discriminant<T> {
436     #[inline]
hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher)437     fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) {
438         ::std::hash::Hash::hash(self, hasher);
439     }
440 }
441 
442 impl<T, CTX> HashStable<CTX> for ::std::ops::RangeInclusive<T>
443 where
444     T: HashStable<CTX>,
445 {
446     #[inline]
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)447     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
448         self.start().hash_stable(ctx, hasher);
449         self.end().hash_stable(ctx, hasher);
450     }
451 }
452 
453 impl<I: vec::Idx, T, CTX> HashStable<CTX> for vec::IndexVec<I, T>
454 where
455     T: HashStable<CTX>,
456 {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)457     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
458         self.len().hash_stable(ctx, hasher);
459         for v in &self.raw {
460             v.hash_stable(ctx, hasher);
461         }
462     }
463 }
464 
465 impl<I: vec::Idx, CTX> HashStable<CTX> for bit_set::BitSet<I> {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)466     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
467         self.words().hash_stable(ctx, hasher);
468     }
469 }
470 
471 impl<R: vec::Idx, C: vec::Idx, CTX> HashStable<CTX> for bit_set::BitMatrix<R, C> {
hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher)472     fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
473         self.words().hash_stable(ctx, hasher);
474     }
475 }
476 
477 impl<T, CTX> HashStable<CTX> for bit_set::FiniteBitSet<T>
478 where
479     T: HashStable<CTX> + bit_set::FiniteBitSetTy,
480 {
hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher)481     fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) {
482         self.0.hash_stable(hcx, hasher);
483     }
484 }
485 
486 impl_stable_hash_via_hash!(::std::path::Path);
487 impl_stable_hash_via_hash!(::std::path::PathBuf);
488 
489 impl<K, V, R, HCX> HashStable<HCX> for ::std::collections::HashMap<K, V, R>
490 where
491     K: ToStableHashKey<HCX> + Eq,
492     V: HashStable<HCX>,
493     R: BuildHasher,
494 {
495     #[inline]
hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher)496     fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
497         hash_stable_hashmap(hcx, hasher, self, ToStableHashKey::to_stable_hash_key);
498     }
499 }
500 
501 impl<K, R, HCX> HashStable<HCX> for ::std::collections::HashSet<K, R>
502 where
503     K: ToStableHashKey<HCX> + Eq,
504     R: BuildHasher,
505 {
hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher)506     fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
507         let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect();
508         keys.sort_unstable();
509         keys.hash_stable(hcx, hasher);
510     }
511 }
512 
513 impl<K, V, HCX> HashStable<HCX> for ::std::collections::BTreeMap<K, V>
514 where
515     K: ToStableHashKey<HCX>,
516     V: HashStable<HCX>,
517 {
hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher)518     fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
519         let mut entries: Vec<_> =
520             self.iter().map(|(k, v)| (k.to_stable_hash_key(hcx), v)).collect();
521         entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
522         entries.hash_stable(hcx, hasher);
523     }
524 }
525 
526 impl<K, HCX> HashStable<HCX> for ::std::collections::BTreeSet<K>
527 where
528     K: ToStableHashKey<HCX>,
529 {
hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher)530     fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
531         let mut keys: Vec<_> = self.iter().map(|k| k.to_stable_hash_key(hcx)).collect();
532         keys.sort_unstable();
533         keys.hash_stable(hcx, hasher);
534     }
535 }
536 
hash_stable_hashmap<HCX, K, V, R, SK, F>( hcx: &mut HCX, hasher: &mut StableHasher, map: &::std::collections::HashMap<K, V, R>, to_stable_hash_key: F, ) where K: Eq, V: HashStable<HCX>, R: BuildHasher, SK: HashStable<HCX> + Ord, F: Fn(&K, &HCX) -> SK,537 pub fn hash_stable_hashmap<HCX, K, V, R, SK, F>(
538     hcx: &mut HCX,
539     hasher: &mut StableHasher,
540     map: &::std::collections::HashMap<K, V, R>,
541     to_stable_hash_key: F,
542 ) where
543     K: Eq,
544     V: HashStable<HCX>,
545     R: BuildHasher,
546     SK: HashStable<HCX> + Ord,
547     F: Fn(&K, &HCX) -> SK,
548 {
549     let mut entries: Vec<_> = map.iter().map(|(k, v)| (to_stable_hash_key(k, hcx), v)).collect();
550     entries.sort_unstable_by(|&(ref sk1, _), &(ref sk2, _)| sk1.cmp(sk2));
551     entries.hash_stable(hcx, hasher);
552 }
553 
554 /// A vector container that makes sure that its items are hashed in a stable
555 /// order.
556 #[derive(Debug)]
557 pub struct StableVec<T>(Vec<T>);
558 
559 impl<T> StableVec<T> {
new(v: Vec<T>) -> Self560     pub fn new(v: Vec<T>) -> Self {
561         StableVec(v)
562     }
563 }
564 
565 impl<T> ::std::ops::Deref for StableVec<T> {
566     type Target = Vec<T>;
567 
deref(&self) -> &Vec<T>568     fn deref(&self) -> &Vec<T> {
569         &self.0
570     }
571 }
572 
573 impl<T, HCX> HashStable<HCX> for StableVec<T>
574 where
575     T: HashStable<HCX> + ToStableHashKey<HCX>,
576 {
hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher)577     fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
578         let StableVec(ref v) = *self;
579 
580         let mut sorted: Vec<_> = v.iter().map(|x| x.to_stable_hash_key(hcx)).collect();
581         sorted.sort_unstable();
582         sorted.hash_stable(hcx, hasher);
583     }
584 }
585