Skip to content

Commit 467a5c6

Browse files
Merge #8347
8347: Minor interning improvements r=jonas-schievink a=jonas-schievink bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents 6e9798a + b57462d commit 467a5c6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

crates/hir_def/src/intern.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use std::{
66
collections::HashMap,
77
fmt::{self, Debug},
8-
hash::{BuildHasherDefault, Hash},
8+
hash::{BuildHasherDefault, Hash, Hasher},
99
ops::Deref,
1010
sync::Arc,
1111
};
@@ -20,7 +20,6 @@ type InternMap<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>;
2020
type Guard<T> =
2121
RwLockWriteGuard<'static, HashMap<Arc<T>, SharedValue<()>, BuildHasherDefault<FxHasher>>>;
2222

23-
#[derive(Hash)]
2423
pub struct Interned<T: Internable + ?Sized> {
2524
arc: Arc<T>,
2625
}
@@ -137,6 +136,13 @@ impl PartialEq for Interned<str> {
137136

138137
impl Eq for Interned<str> {}
139138

139+
impl<T: Internable + ?Sized> Hash for Interned<T> {
140+
fn hash<H: Hasher>(&self, state: &mut H) {
141+
// NOTE: Cast disposes vtable pointer / slice/str length.
142+
state.write_usize(Arc::as_ptr(&self.arc) as *const () as usize)
143+
}
144+
}
145+
140146
impl<T: Internable + ?Sized> AsRef<T> for Interned<T> {
141147
#[inline]
142148
fn as_ref(&self) -> &T {
@@ -185,7 +191,10 @@ pub trait Internable: Hash + Eq + 'static {
185191
fn storage() -> &'static InternStorage<Self>;
186192
}
187193

188-
macro_rules! impl_internable {
194+
/// Implements `Internable` for a given list of types, making them usable with `Interned`.
195+
#[macro_export]
196+
#[doc(hidden)]
197+
macro_rules! _impl_internable {
189198
( $($t:path),+ $(,)? ) => { $(
190199
impl Internable for $t {
191200
fn storage() -> &'static InternStorage<Self> {
@@ -196,10 +205,12 @@ macro_rules! impl_internable {
196205
)+ };
197206
}
198207

208+
pub use crate::_impl_internable as impl_internable;
209+
199210
impl_internable!(
200211
crate::type_ref::TypeRef,
201212
crate::type_ref::TraitRef,
202213
crate::path::ModPath,
203214
GenericParams,
204-
str
215+
str,
205216
);

0 commit comments

Comments
 (0)