1use std::ffi::OsStr;
2use std::hash::{BuildHasher, Hasher};
3use std::marker::PhantomData;
4use std::num::NonZero;
5
6use rustc_hashes::{Hash64, Hash128};
7use rustc_index::{Idx, IndexSlice, IndexVec, bit_set};
8use rustc_macros::StableHash;
9pub use rustc_stable_hash::{
10 FromStableHash, SipHasher128Hash as StableHasherHash, StableSipHasher128 as StableHasher,
11};
12use smallvec::SmallVec;
13use thin_vec::ThinVec;
14
15use crate::fingerprint::Fingerprint;
16
17#[cfg(test)]
18mod tests;
19
20pub trait StableHashCtxt {
25 fn stable_hash_span(&mut self, span: RawSpan, hasher: &mut StableHasher);
27
28 fn def_path_hash(&self, def_id: RawDefId) -> Fingerprint;
30
31 fn stable_hash_controls(&self) -> StableHashControls;
33
34 fn assert_default_stable_hash_controls(&self, msg: &str);
37}
38
39pub struct DummyStableHashCtxt;
42
43impl StableHashCtxt for DummyStableHashCtxt {
44 fn stable_hash_span(&mut self, _span: RawSpan, _hasher: &mut StableHasher) {
45 {
::core::panicking::panic_fmt(format_args!("method on dummy stable-hash context was unexpectedly called"));
};panic!("method on dummy stable-hash context was unexpectedly called");
46 }
47 fn def_path_hash(&self, _def_id: RawDefId) -> Fingerprint {
48 {
::core::panicking::panic_fmt(format_args!("method on dummy stable-hash context was unexpectedly called"));
};panic!("method on dummy stable-hash context was unexpectedly called");
49 }
50 fn stable_hash_controls(&self) -> StableHashControls {
51 {
::core::panicking::panic_fmt(format_args!("method on dummy stable-hash context was unexpectedly called"));
};panic!("method on dummy stable-hash context was unexpectedly called");
52 }
53 fn assert_default_stable_hash_controls(&self, _msg: &str) {
54 {
::core::panicking::panic_fmt(format_args!("method on dummy stable-hash context was unexpectedly called"));
};panic!("method on dummy stable-hash context was unexpectedly called");
55 }
56}
57
58pub struct RawSpan(pub u64);
61
62pub struct RawDefId(pub u32, pub u32);
65
66pub trait StableHash {
105 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher);
106}
107
108pub trait ToStableHashKey {
112 type KeyType: Ord + Sized + StableHash;
113 fn to_stable_hash_key<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx) -> Self::KeyType;
114}
115
116pub trait StableOrd: Ord {
146 const CAN_USE_UNSTABLE_SORT: bool;
147
148 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: ();
151}
152
153impl<T: StableOrd> StableOrd for &T {
154 const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
155
156 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
160}
161
162pub trait StableCompare {
173 const CAN_USE_UNSTABLE_SORT: bool;
174
175 fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering;
176}
177
178impl<T: StableOrd> StableCompare for T {
181 const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
182
183 fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
184 self.cmp(other)
185 }
186}
187
188macro_rules! impl_stable_traits_for_int {
191 (
192 $ty:ty, $write_ty_method:ident
193 ) => {
194 impl StableHash for $ty {
195 #[inline]
196 fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
197 hasher.$write_ty_method(*self);
198 }
199 }
200
201 impl StableOrd for $ty {
202 const CAN_USE_UNSTABLE_SORT: bool = true;
203
204 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
207 }
208 };
209}
210
211impl StableHash for u8 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_u8(*self);
}
}
impl StableOrd for u8 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(u8, write_u8);
212impl StableHash for u16 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_u16(*self);
}
}
impl StableOrd for u16 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(u16, write_u16);
213impl StableHash for u32 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_u32(*self);
}
}
impl StableOrd for u32 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(u32, write_u32);
214impl StableHash for u64 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_u64(*self);
}
}
impl StableOrd for u64 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(u64, write_u64);
215impl StableHash for u128 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_u128(*self);
}
}
impl StableOrd for u128 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(u128, write_u128);
216impl StableHash for usize {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_usize(*self);
}
}
impl StableOrd for usize {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(usize, write_usize);
217
218impl StableHash for i8 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_i8(*self);
}
}
impl StableOrd for i8 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(i8, write_i8);
219impl StableHash for i16 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_i16(*self);
}
}
impl StableOrd for i16 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(i16, write_i16);
220impl StableHash for i32 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_i32(*self);
}
}
impl StableOrd for i32 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(i32, write_i32);
221impl StableHash for i64 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_i64(*self);
}
}
impl StableOrd for i64 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(i64, write_i64);
222impl StableHash for i128 {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_i128(*self);
}
}
impl StableOrd for i128 {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(i128, write_i128);
223impl StableHash for isize {
#[inline]
fn stable_hash<Hcx>(&self, _: &mut Hcx, hasher: &mut StableHasher) {
hasher.write_isize(*self);
}
}
impl StableOrd for isize {
const CAN_USE_UNSTABLE_SORT: bool = true;
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
}impl_stable_traits_for_int!(isize, write_isize);
224
225impl StableHash for char {
226 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
227 u32::from(*self).stable_hash(hcx, hasher);
228 }
229}
230
231impl StableHash for () {
232 fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {
233 }
235}
236
237impl StableHash for Hash64 {
238 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
239 self.as_u64().stable_hash(hcx, hasher);
240 }
241}
242
243impl StableHash for Hash128 {
244 #[inline]
245 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
246 self.as_u128().stable_hash(hcx, hasher);
247 }
248}
249
250impl StableOrd for Hash128 {
251 const CAN_USE_UNSTABLE_SORT: bool = true;
252
253 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
256}
257
258impl StableHash for ! {
259 fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {
260 ::core::panicking::unreachable_default_msg()unreachable!()
261 }
262}
263
264impl<T> StableHash for PhantomData<T> {
265 fn stable_hash<Hcx>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {}
266}
267
268impl<T: StableHash + std::num::ZeroablePrimitive> StableHash for NonZero<T> {
269 #[inline]
270 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
271 self.get().stable_hash(hcx, hasher)
272 }
273}
274
275impl StableHash for f32 {
276 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
277 let val: u32 = self.to_bits();
278 val.stable_hash(hcx, hasher);
279 }
280}
281
282impl StableHash for f64 {
283 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
284 let val: u64 = self.to_bits();
285 val.stable_hash(hcx, hasher);
286 }
287}
288
289impl StableHash for ::std::cmp::Ordering {
290 #[inline]
291 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
292 (*self as i8).stable_hash(hcx, hasher);
293 }
294}
295
296impl<T1: StableHash> StableHash for (T1,) {
297 #[inline]
298 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
299 let (ref _0,) = *self;
300 _0.stable_hash(hcx, hasher);
301 }
302}
303
304impl<T1: StableHash, T2: StableHash> StableHash for (T1, T2) {
305 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
306 let (ref _0, ref _1) = *self;
307 _0.stable_hash(hcx, hasher);
308 _1.stable_hash(hcx, hasher);
309 }
310}
311
312impl<T1: StableOrd, T2: StableOrd> StableOrd for (T1, T2) {
313 const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT;
314
315 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
318}
319
320impl<T1, T2, T3> StableHash for (T1, T2, T3)
321where
322 T1: StableHash,
323 T2: StableHash,
324 T3: StableHash,
325{
326 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
327 let (ref _0, ref _1, ref _2) = *self;
328 _0.stable_hash(hcx, hasher);
329 _1.stable_hash(hcx, hasher);
330 _2.stable_hash(hcx, hasher);
331 }
332}
333
334impl<T1: StableOrd, T2: StableOrd, T3: StableOrd> StableOrd for (T1, T2, T3) {
335 const CAN_USE_UNSTABLE_SORT: bool =
336 T1::CAN_USE_UNSTABLE_SORT && T2::CAN_USE_UNSTABLE_SORT && T3::CAN_USE_UNSTABLE_SORT;
337
338 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
341}
342
343impl<T1, T2, T3, T4> StableHash for (T1, T2, T3, T4)
344where
345 T1: StableHash,
346 T2: StableHash,
347 T3: StableHash,
348 T4: StableHash,
349{
350 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
351 let (ref _0, ref _1, ref _2, ref _3) = *self;
352 _0.stable_hash(hcx, hasher);
353 _1.stable_hash(hcx, hasher);
354 _2.stable_hash(hcx, hasher);
355 _3.stable_hash(hcx, hasher);
356 }
357}
358
359impl<T1: StableOrd, T2: StableOrd, T3: StableOrd, T4: StableOrd> StableOrd for (T1, T2, T3, T4) {
360 const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT
361 && T2::CAN_USE_UNSTABLE_SORT
362 && T3::CAN_USE_UNSTABLE_SORT
363 && T4::CAN_USE_UNSTABLE_SORT;
364
365 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
368}
369
370impl<T: StableHash> StableHash for [T] {
371 default fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
372 self.len().stable_hash(hcx, hasher);
373 for item in self {
374 item.stable_hash(hcx, hasher);
375 }
376 }
377}
378
379impl StableHash for [u8] {
380 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
381 self.len().stable_hash(hcx, hasher);
382 hasher.write(self);
383 }
384}
385
386impl<T: StableHash> StableHash for Vec<T> {
387 #[inline]
388 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
389 self[..].stable_hash(hcx, hasher);
390 }
391}
392
393impl<K, V, R> StableHash for indexmap::IndexMap<K, V, R>
394where
395 K: StableHash,
396 V: StableHash,
397 R: BuildHasher,
398{
399 #[inline]
400 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
401 self.len().stable_hash(hcx, hasher);
402 for kv in self {
403 kv.stable_hash(hcx, hasher);
404 }
405 }
406}
407
408impl<K, R> StableHash for indexmap::IndexSet<K, R>
409where
410 K: StableHash,
411 R: BuildHasher,
412{
413 #[inline]
414 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
415 self.len().stable_hash(hcx, hasher);
416 for key in self {
417 key.stable_hash(hcx, hasher);
418 }
419 }
420}
421
422impl<A, const N: usize> StableHash for SmallVec<[A; N]>
423where
424 A: StableHash,
425{
426 #[inline]
427 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
428 self[..].stable_hash(hcx, hasher);
429 }
430}
431
432impl<T: StableHash> StableHash for ThinVec<T> {
433 #[inline]
434 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
435 self[..].stable_hash(hcx, hasher);
436 }
437}
438
439impl<T: ?Sized + StableHash> StableHash for Box<T> {
440 #[inline]
441 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
442 (**self).stable_hash(hcx, hasher);
443 }
444}
445
446impl<T: ?Sized + StableHash> StableHash for ::std::rc::Rc<T> {
447 #[inline]
448 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
449 (**self).stable_hash(hcx, hasher);
450 }
451}
452
453impl<T: ?Sized + StableHash> StableHash for ::std::sync::Arc<T> {
454 #[inline]
455 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
456 (**self).stable_hash(hcx, hasher);
457 }
458}
459
460impl StableHash for str {
461 #[inline]
462 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
463 self.as_bytes().stable_hash(hcx, hasher);
464 }
465}
466
467impl StableOrd for &str {
468 const CAN_USE_UNSTABLE_SORT: bool = true;
469
470 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
473}
474
475impl StableHash for String {
476 #[inline]
477 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
478 self[..].stable_hash(hcx, hasher);
479 }
480}
481
482impl StableOrd for String {
483 const CAN_USE_UNSTABLE_SORT: bool = true;
484
485 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
488}
489
490impl StableHash for bool {
491 #[inline]
492 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
493 (if *self { 1u8 } else { 0u8 }).stable_hash(hcx, hasher);
494 }
495}
496
497impl StableOrd for bool {
498 const CAN_USE_UNSTABLE_SORT: bool = true;
499
500 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
502}
503
504impl<T> StableHash for Option<T>
505where
506 T: StableHash,
507{
508 #[inline]
509 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
510 if let Some(ref value) = *self {
511 1u8.stable_hash(hcx, hasher);
512 value.stable_hash(hcx, hasher);
513 } else {
514 0u8.stable_hash(hcx, hasher);
515 }
516 }
517}
518
519impl<T: StableOrd> StableOrd for Option<T> {
520 const CAN_USE_UNSTABLE_SORT: bool = T::CAN_USE_UNSTABLE_SORT;
521
522 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
524}
525
526impl<T1, T2> StableHash for Result<T1, T2>
527where
528 T1: StableHash,
529 T2: StableHash,
530{
531 #[inline]
532 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
533 #[derive(const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for Tag {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Tag::Ok => 0u8.stable_hash(__hcx, __hasher),
Tag::Err => 1u8.stable_hash(__hcx, __hasher),
}
match *self { Tag::Ok => {} Tag::Err => {} }
}
}
};StableHash)]
534 enum Tag {
535 Ok,
536 Err,
537 }
538 match *self {
539 Ok(ref x) => {
540 Tag::Ok.stable_hash(hcx, hasher);
541 x.stable_hash(hcx, hasher);
542 }
543 Err(ref x) => {
544 Tag::Err.stable_hash(hcx, hasher);
545 x.stable_hash(hcx, hasher);
546 }
547 }
548 }
549}
550
551impl<'a, T> StableHash for &'a T
552where
553 T: StableHash + ?Sized,
554{
555 #[inline]
556 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
557 (**self).stable_hash(hcx, hasher);
558 }
559}
560
561impl<T> !StableHash for std::mem::Discriminant<T> {}
574
575impl<T> StableHash for ::std::range::RangeInclusive<T>
576where
577 T: StableHash,
578{
579 #[inline]
580 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
581 self.start.stable_hash(hcx, hasher);
582 self.last.stable_hash(hcx, hasher);
583 }
584}
585
586impl<I: Idx, T> StableHash for IndexSlice<I, T>
587where
588 T: StableHash,
589{
590 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
591 self.len().stable_hash(hcx, hasher);
592 for v in &self.raw {
593 v.stable_hash(hcx, hasher);
594 }
595 }
596}
597
598impl<I: Idx, T> StableHash for IndexVec<I, T>
599where
600 T: StableHash,
601{
602 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
603 self.len().stable_hash(hcx, hasher);
604 for v in &self.raw {
605 v.stable_hash(hcx, hasher);
606 }
607 }
608}
609
610impl<I: Idx> StableHash for bit_set::DenseBitSet<I> {
611 fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, hasher: &mut StableHasher) {
612 bit_set::DenseBitSet::stable_hash_inner(self, hasher);
613 }
614}
615
616impl<R: Idx, C: Idx> StableHash for bit_set::BitMatrix<R, C> {
617 fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, hasher: &mut StableHasher) {
618 bit_set::BitMatrix::stable_hash_inner(self, hasher);
619 }
620}
621
622impl StableHash for OsStr {
623 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
624 self.as_encoded_bytes().stable_hash(hcx, hasher);
625 }
626}
627
628impl StableHash for std::path::Path {
629 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
630 use std::path::{Component, Prefix};
631
632 #[derive(const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for Tag {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Tag::StartOfPath => 0u8.stable_hash(__hcx, __hasher),
Tag::PrefixVerbatim => 1u8.stable_hash(__hcx, __hasher),
Tag::PrefixVerbatimUNC => 2u8.stable_hash(__hcx, __hasher),
Tag::PrefixVerbatimDisk => 3u8.stable_hash(__hcx, __hasher),
Tag::PrefixDeviceNS => 4u8.stable_hash(__hcx, __hasher),
Tag::PrefixUNC => 5u8.stable_hash(__hcx, __hasher),
Tag::PrefixDisk => 6u8.stable_hash(__hcx, __hasher),
Tag::RootDir => 7u8.stable_hash(__hcx, __hasher),
Tag::CurDir => 8u8.stable_hash(__hcx, __hasher),
Tag::ParentDir => 9u8.stable_hash(__hcx, __hasher),
Tag::Normal => 10u8.stable_hash(__hcx, __hasher),
Tag::EndOfPath => 11u8.stable_hash(__hcx, __hasher),
}
match *self {
Tag::StartOfPath => {}
Tag::PrefixVerbatim => {}
Tag::PrefixVerbatimUNC => {}
Tag::PrefixVerbatimDisk => {}
Tag::PrefixDeviceNS => {}
Tag::PrefixUNC => {}
Tag::PrefixDisk => {}
Tag::RootDir => {}
Tag::CurDir => {}
Tag::ParentDir => {}
Tag::Normal => {}
Tag::EndOfPath => {}
}
}
}
};StableHash)]
634 enum Tag {
635 StartOfPath,
636 PrefixVerbatim,
637 PrefixVerbatimUNC,
638 PrefixVerbatimDisk,
639 PrefixDeviceNS,
640 PrefixUNC,
641 PrefixDisk,
642 RootDir,
643 CurDir,
644 ParentDir,
645 Normal,
646 EndOfPath,
647 }
648
649 Tag::StartOfPath.stable_hash(hcx, hasher);
651
652 for (i, component) in self.components().enumerate() {
653 i.stable_hash(hcx, hasher);
656 match component {
657 Component::Prefix(prefix) => match prefix.kind() {
658 Prefix::Verbatim(os_str) => {
659 Tag::PrefixVerbatim.stable_hash(hcx, hasher);
660 os_str.stable_hash(hcx, hasher);
661 }
662 Prefix::VerbatimUNC(host, share) => {
663 Tag::PrefixVerbatimUNC.stable_hash(hcx, hasher);
664 host.stable_hash(hcx, hasher);
665 share.stable_hash(hcx, hasher);
666 }
667 Prefix::VerbatimDisk(letter) => {
668 Tag::PrefixVerbatimDisk.stable_hash(hcx, hasher);
669 letter.stable_hash(hcx, hasher);
670 }
671 Prefix::DeviceNS(os_str) => {
672 Tag::PrefixDeviceNS.stable_hash(hcx, hasher);
673 os_str.stable_hash(hcx, hasher);
674 }
675 Prefix::UNC(host, share) => {
676 Tag::PrefixUNC.stable_hash(hcx, hasher);
677 host.stable_hash(hcx, hasher);
678 share.stable_hash(hcx, hasher);
679 }
680 Prefix::Disk(letter) => {
681 Tag::PrefixDisk.stable_hash(hcx, hasher);
682 letter.stable_hash(hcx, hasher);
683 }
684 },
685 Component::RootDir => Tag::RootDir.stable_hash(hcx, hasher),
686 Component::CurDir => Tag::CurDir.stable_hash(hcx, hasher),
687 Component::ParentDir => Tag::ParentDir.stable_hash(hcx, hasher),
688 Component::Normal(os_str) => {
689 Tag::Normal.stable_hash(hcx, hasher);
690 os_str.stable_hash(hcx, hasher);
691 }
692 }
693 }
694
695 Tag::EndOfPath.stable_hash(hcx, hasher);
698 }
699}
700
701impl StableOrd for std::path::Path {
702 const CAN_USE_UNSTABLE_SORT: bool = true;
704 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
706}
707
708impl StableHash for std::path::PathBuf {
709 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
710 self.as_path().stable_hash(hcx, hasher);
711 }
712}
713
714impl StableOrd for std::path::PathBuf {
715 const CAN_USE_UNSTABLE_SORT: bool = <std::path::Path as StableOrd>::CAN_USE_UNSTABLE_SORT;
716 const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
718}
719
720impl<V> !StableHash for std::collections::HashSet<V> {}
724impl<K, V> !StableHash for std::collections::HashMap<K, V> {}
725
726impl<K, V> StableHash for ::std::collections::BTreeMap<K, V>
727where
728 K: StableHash + StableOrd,
729 V: StableHash,
730{
731 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
732 self.len().stable_hash(hcx, hasher);
733 for entry in self.iter() {
734 entry.stable_hash(hcx, hasher);
735 }
736 }
737}
738
739impl<K> StableHash for ::std::collections::BTreeSet<K>
740where
741 K: StableHash + StableOrd,
742{
743 fn stable_hash<Hcx: StableHashCtxt>(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
744 self.len().stable_hash(hcx, hasher);
745 for entry in self.iter() {
746 entry.stable_hash(hcx, hasher);
747 }
748 }
749}
750
751#[derive(#[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for StableHashControls {
}
#[automatically_derived]impl ::core::clone::Clone for StableHashControls {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]impl ::core::marker::Copy for StableHashControls { }Copy, #[automatically_derived]impl ::core::hash::Hash for StableHashControls {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.hash_spans, state)
}
}Hash, #[automatically_derived]impl ::core::cmp::Eq for StableHashControls {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<bool>;
}
}Eq, #[automatically_derived]impl ::core::marker::StructuralPartialEq for
StableHashControls {
}
#[automatically_derived]impl ::core::cmp::PartialEq for StableHashControls {
#[inline]
fn eq(&self, other: &Self) -> bool { self.hash_spans == other.hash_spans }
}PartialEq, #[automatically_derived]impl ::core::fmt::Debug for StableHashControls {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"StableHashControls hash_spans", &&self.hash_spans)
}
}Debug)]
759pub struct StableHashControls {
760 pub hash_spans: bool,
761}