From a0b50bcf1e18c5d909e8791e06284b9787076e8b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 5 Apr 2021 16:59:03 +0200 Subject: Make `impl_internable!` macro public --- crates/hir_def/src/intern.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/intern.rs b/crates/hir_def/src/intern.rs index 2467e9299..0d5419be5 100644 --- a/crates/hir_def/src/intern.rs +++ b/crates/hir_def/src/intern.rs @@ -185,7 +185,10 @@ pub trait Internable: Hash + Eq + 'static { fn storage() -> &'static InternStorage; } -macro_rules! impl_internable { +/// Implements `Internable` for a given list of types, making them usable with `Interned`. +#[macro_export] +#[doc(hidden)] +macro_rules! _impl_internable { ( $($t:path),+ $(,)? ) => { $( impl Internable for $t { fn storage() -> &'static InternStorage { @@ -196,10 +199,12 @@ macro_rules! impl_internable { )+ }; } +pub use crate::_impl_internable as impl_internable; + impl_internable!( crate::type_ref::TypeRef, crate::type_ref::TraitRef, crate::path::ModPath, GenericParams, - str + str, ); -- cgit v1.2.3 From b57462d60dbebedcfac00544d5ff1592c5117c1f Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 5 Apr 2021 17:07:53 +0200 Subject: `Interned`: Only hash the pointer --- crates/hir_def/src/intern.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/intern.rs b/crates/hir_def/src/intern.rs index 0d5419be5..abc304ef0 100644 --- a/crates/hir_def/src/intern.rs +++ b/crates/hir_def/src/intern.rs @@ -5,7 +5,7 @@ use std::{ collections::HashMap, fmt::{self, Debug}, - hash::{BuildHasherDefault, Hash}, + hash::{BuildHasherDefault, Hash, Hasher}, ops::Deref, sync::Arc, }; @@ -20,7 +20,6 @@ type InternMap = DashMap, (), BuildHasherDefault>; type Guard = RwLockWriteGuard<'static, HashMap, SharedValue<()>, BuildHasherDefault>>; -#[derive(Hash)] pub struct Interned { arc: Arc, } @@ -137,6 +136,13 @@ impl PartialEq for Interned { impl Eq for Interned {} +impl Hash for Interned { + fn hash(&self, state: &mut H) { + // NOTE: Cast disposes vtable pointer / slice/str length. + state.write_usize(Arc::as_ptr(&self.arc) as *const () as usize) + } +} + impl AsRef for Interned { #[inline] fn as_ref(&self) -> &T { -- cgit v1.2.3