From 2e0ef701c2e8ecb03ccebdc268980ec6d62a4a39 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 5 Apr 2022 16:42:47 +0000 Subject: Document and rename the new wrapper type --- compiler/rustc_data_structures/src/intern.rs | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'compiler/rustc_data_structures/src') diff --git a/compiler/rustc_data_structures/src/intern.rs b/compiler/rustc_data_structures/src/intern.rs index bd62f30372e..009b5d5340a 100644 --- a/compiler/rustc_data_structures/src/intern.rs +++ b/compiler/rustc_data_structures/src/intern.rs @@ -115,34 +115,41 @@ pub trait InternedHashingContext { fn with_def_path_and_no_spans(&mut self, f: impl FnOnce(&mut Self)); } +/// A helper type that you can wrap round your own type in order to automatically +/// cache the stable hash on creation and not recompute it whenever the stable hash +/// of the type is computed. +/// This is only done in incremental mode. You can also opt out of caching by using +/// StableHash::ZERO for the hash, in which case the hash gets computed each time. +/// This is useful if you have values that you intern but never (can?) use for stable +/// hashing. #[derive(Copy, Clone)] -pub struct InTy { +pub struct WithStableHash { pub internee: T, pub stable_hash: Fingerprint, } -impl PartialEq for InTy { +impl PartialEq for WithStableHash { #[inline] fn eq(&self, other: &Self) -> bool { self.internee.eq(&other.internee) } } -impl Eq for InTy {} +impl Eq for WithStableHash {} -impl PartialOrd for InTy { - fn partial_cmp(&self, other: &InTy) -> Option { +impl PartialOrd for WithStableHash { + fn partial_cmp(&self, other: &WithStableHash) -> Option { Some(self.internee.cmp(&other.internee)) } } -impl Ord for InTy { - fn cmp(&self, other: &InTy) -> Ordering { +impl Ord for WithStableHash { + fn cmp(&self, other: &WithStableHash) -> Ordering { self.internee.cmp(&other.internee) } } -impl Deref for InTy { +impl Deref for WithStableHash { type Target = T; #[inline] @@ -151,14 +158,14 @@ impl Deref for InTy { } } -impl Hash for InTy { +impl Hash for WithStableHash { #[inline] fn hash(&self, s: &mut H) { self.internee.hash(s) } } -impl, CTX: InternedHashingContext> HashStable for InTy { +impl, CTX: InternedHashingContext> HashStable for WithStableHash { fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { if self.stable_hash == Fingerprint::ZERO || cfg!(debug_assertions) { // No cached hash available. This can only mean that incremental is disabled. -- cgit 1.4.1-3-g733a5