Interned<T>: Only hash the pointer

This commit is contained in:
Jonas Schievink 2021-04-05 17:07:53 +02:00
parent a0b50bcf1e
commit b57462d60d

View file

@ -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<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>;
type Guard<T> =
RwLockWriteGuard<'static, HashMap<Arc<T>, SharedValue<()>, BuildHasherDefault<FxHasher>>>;
#[derive(Hash)]
pub struct Interned<T: Internable + ?Sized> {
arc: Arc<T>,
}
@ -137,6 +136,13 @@ impl PartialEq for Interned<str> {
impl Eq for Interned<str> {}
impl<T: Internable + ?Sized> Hash for Interned<T> {
fn hash<H: Hasher>(&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<T: Internable + ?Sized> AsRef<T> for Interned<T> {
#[inline]
fn as_ref(&self) -> &T {