Remove unnecessary region, relax Sized bounds

This commit is contained in:
Jonas Schievink 2021-04-01 22:24:40 +02:00
parent 39d992ef55
commit afd83e0686

View file

@ -16,7 +16,7 @@ use rustc_hash::FxHasher;
type InternMap<T> = DashMap<Arc<T>, (), BuildHasherDefault<FxHasher>>;
#[derive(Hash)]
pub struct Interned<T: Internable> {
pub struct Interned<T: Internable + ?Sized> {
arc: Arc<T>,
}
@ -52,7 +52,7 @@ impl<T: Internable> Interned<T> {
}
}
impl<T: Internable> Drop for Interned<T> {
impl<T: Internable + ?Sized> Drop for Interned<T> {
fn drop(&mut self) {
// When the last `Ref` is dropped, remove the object from the global map.
if Arc::strong_count(&self.arc) == 2 {
@ -83,23 +83,23 @@ impl<T: Internable> Drop for Interned<T> {
}
/// Compares interned `Ref`s using pointer equality.
impl<T: Internable> PartialEq for Interned<T> {
impl<T: Internable + ?Sized> PartialEq for Interned<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.arc, &other.arc)
}
}
impl<T: Internable> Eq for Interned<T> {}
impl<T: Internable + ?Sized> Eq for Interned<T> {}
impl<T: Internable> AsRef<T> for Interned<T> {
impl<T: Internable + ?Sized> AsRef<T> for Interned<T> {
#[inline]
fn as_ref(&self) -> &T {
&self.arc
}
}
impl<T: Internable> Deref for Interned<T> {
impl<T: Internable + ?Sized> Deref for Interned<T> {
type Target = T;
#[inline]
@ -108,40 +108,38 @@ impl<T: Internable> Deref for Interned<T> {
}
}
impl<T: Internable> Clone for Interned<T> {
impl<T: Internable + ?Sized> Clone for Interned<T> {
fn clone(&self) -> Self {
Self { arc: self.arc.clone() }
}
}
impl<T: Debug + Internable> Debug for Interned<T> {
impl<T: Debug + Internable + ?Sized> Debug for Interned<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*self.arc).fmt(f)
}
}
pub struct InternStorage<T> {
pub struct InternStorage<T: ?Sized> {
map: OnceCell<InternMap<T>>,
}
impl<T> InternStorage<T> {
impl<T: ?Sized> InternStorage<T> {
pub const fn new() -> Self {
Self { map: OnceCell::new() }
}
}
impl<T: Internable> InternStorage<T> {
impl<T: Internable + ?Sized> InternStorage<T> {
fn get(&self) -> &InternMap<T> {
self.map.get_or_init(DashMap::default)
}
}
pub trait Internable: Hash + Eq + Sized + 'static {
pub trait Internable: Hash + Eq + 'static {
fn storage() -> &'static InternStorage<Self>;
}
// region:`Internable` implementations
macro_rules! impl_internable {
( $($t:ty),+ $(,)? ) => { $(
impl Internable for $t {
@ -154,5 +152,3 @@ macro_rules! impl_internable {
}
impl_internable!(crate::type_ref::TypeRef, crate::type_ref::TraitRef, crate::path::ModPath);
// endregion