From f12322aa2a858533cc413048f2f42350b596e515 Mon Sep 17 00:00:00 2001 From: Jacob Lifshay Date: Mon, 7 Oct 2024 21:33:56 -0700 Subject: [PATCH] remove interning contexts --- .../src/hdl_bundle.rs | 4 +- .../src/hdl_type_common.rs | 6 +- crates/fayalite/src/array.rs | 6 +- crates/fayalite/src/bundle.rs | 2 +- crates/fayalite/src/expr/ops.rs | 8 +- crates/fayalite/src/int.rs | 2 +- crates/fayalite/src/intern.rs | 575 +++++------------- crates/fayalite/src/ty.rs | 2 +- 8 files changed, 154 insertions(+), 451 deletions(-) diff --git a/crates/fayalite-proc-macros-impl/src/hdl_bundle.rs b/crates/fayalite-proc-macros-impl/src/hdl_bundle.rs index f3a589a..b13aa08 100644 --- a/crates/fayalite-proc-macros-impl/src/hdl_bundle.rs +++ b/crates/fayalite-proc-macros-impl/src/hdl_bundle.rs @@ -686,7 +686,7 @@ impl ToTokens for ParsedBundle { let __retval = #mask_type_match_variant_ident { #(#match_variant_body_fields)* }; - ::fayalite::intern::Interned::<_>::into_inner(::fayalite::intern::Intern::intern_sized(__retval)) + ::fayalite::intern::Interned::into_inner(::fayalite::intern::Intern::intern_sized(__retval)) } } #[automatically_derived] @@ -761,7 +761,7 @@ impl ToTokens for ParsedBundle { let __retval = #match_variant_ident { #(#match_variant_body_fields)* }; - ::fayalite::intern::Interned::<_>::into_inner(::fayalite::intern::Intern::intern_sized(__retval)) + ::fayalite::intern::Interned::into_inner(::fayalite::intern::Intern::intern_sized(__retval)) } } } diff --git a/crates/fayalite-proc-macros-impl/src/hdl_type_common.rs b/crates/fayalite-proc-macros-impl/src/hdl_type_common.rs index e7561fe..239f32b 100644 --- a/crates/fayalite-proc-macros-impl/src/hdl_type_common.rs +++ b/crates/fayalite-proc-macros-impl/src/hdl_type_common.rs @@ -2718,7 +2718,7 @@ impl ParsedGenerics { type Output = #next_target #next_type_generics; fn index(&self, #param_token: #index_type) -> &Self::Output { - ::fayalite::intern::Interned::<_>::into_inner( + ::fayalite::intern::Interned::into_inner( ::fayalite::intern::Intern::intern_sized(#output_expr), ) } @@ -2778,7 +2778,7 @@ impl ParsedGenerics { type Output = #next_target #next_target_args; fn index(&self, #param_token: #param_ident) -> &Self::Output { - ::fayalite::intern::Interned::<_>::into_inner( + ::fayalite::intern::Interned::into_inner( ::fayalite::intern::Intern::intern_sized(#output_expr), ) } @@ -2848,7 +2848,7 @@ impl ParsedGenerics { type Output = #next_target #next_target_args; fn index(&self, #param_token: __Param) -> &Self::Output { - ::fayalite::intern::Interned::<_>::into_inner( + ::fayalite::intern::Interned::into_inner( ::fayalite::intern::Intern::intern_sized(#output_expr), ) } diff --git a/crates/fayalite/src/array.rs b/crates/fayalite/src/array.rs index d543322..a3aea79 100644 --- a/crates/fayalite/src/array.rs +++ b/crates/fayalite/src/array.rs @@ -187,7 +187,7 @@ impl TypeWithDeref for ArrayType { let base = Expr::as_dyn_array(*this); let base_ty = Expr::ty(base); let retval = Vec::from_iter((0..base_ty.len()).map(|i| ArrayIndex::new(base, i).to_expr())); - Interned::<_>::into_inner(Intern::intern_sized( + Interned::into_inner(Intern::intern_sized( Len::ArrayMatch::::try_from(retval) .ok() .expect("unreachable"), @@ -202,7 +202,7 @@ impl Index for ArrayWithoutGenerics { type Output = ArrayWithoutLen; fn index(&self, element: T) -> &Self::Output { - Interned::<_>::into_inner(Intern::intern_sized(ArrayWithoutLen { element })) + Interned::into_inner(Intern::intern_sized(ArrayWithoutLen { element })) } } @@ -215,6 +215,6 @@ impl Index for ArrayWithoutLen { type Output = ArrayType; fn index(&self, len: L) -> &Self::Output { - Interned::<_>::into_inner(Intern::intern_sized(ArrayType::new(self.element, len))) + Interned::into_inner(Intern::intern_sized(ArrayType::new(self.element, len))) } } diff --git a/crates/fayalite/src/bundle.rs b/crates/fayalite/src/bundle.rs index 95c87f9..199c2b2 100644 --- a/crates/fayalite/src/bundle.rs +++ b/crates/fayalite/src/bundle.rs @@ -377,7 +377,7 @@ macro_rules! impl_tuples { impl<$($T: Type,)*> TypeWithDeref for ($($T,)*) { fn expr_deref(this: &Expr) -> &Self::MatchVariant { let _ = this; - Interned::<_>::into_inner(($(Expr::field(*this, stringify!($num)),)*).intern_sized()) + Interned::into_inner(($(Expr::field(*this, stringify!($num)),)*).intern_sized()) } } impl<$($T: StaticType,)*> StaticType for ($($T,)*) { diff --git a/crates/fayalite/src/expr/ops.rs b/crates/fayalite/src/expr/ops.rs index 6069f20..3579641 100644 --- a/crates/fayalite/src/expr/ops.rs +++ b/crates/fayalite/src/expr/ops.rs @@ -2057,7 +2057,7 @@ impl ExprIndex for ArrayType, index: usize) -> &Expr { - Interned::<_>::into_inner( + Interned::into_inner( ArrayIndex::::new(Expr::as_dyn_array(*this), index) .to_expr() .intern_sized(), @@ -2154,7 +2154,7 @@ impl ExprIndex>> type Output = ElementType; fn expr_index(this: &Expr, index: Expr>) -> &Expr { - Interned::<_>::into_inner( + Interned::into_inner( DynArrayIndex::::new(Expr::as_dyn_array(*this), Expr::as_dyn_int(index)) .to_expr() .intern_sized(), @@ -2279,7 +2279,7 @@ macro_rules! impl_int_slice { let base = Expr::as_dyn_int(*this); let base_ty = Expr::ty(base); let range = base_ty.slice_index_to_range(index); - Interned::<_>::into_inner($name::new(base, range).to_expr().intern_sized()) + Interned::into_inner($name::new(base, range).to_expr().intern_sized()) } } @@ -2291,7 +2291,7 @@ macro_rules! impl_int_slice { let base = Expr::as_dyn_int(*this); let base_ty = Expr::ty(base); assert!(index < base_ty.width()); - Interned::<_>::into_inner( + Interned::into_inner( $name::new(base, index..(index + 1)) .to_expr() .cast_to_static::() diff --git a/crates/fayalite/src/int.rs b/crates/fayalite/src/int.rs index e0d258a..f529198 100644 --- a/crates/fayalite/src/int.rs +++ b/crates/fayalite/src/int.rs @@ -301,7 +301,7 @@ macro_rules! impl_int { type Output = $name; fn index(&self, width: Width) -> &Self::Output { - Interned::<_>::into_inner(Intern::intern_sized($name::new(width))) + Interned::into_inner(Intern::intern_sized($name::new(width))) } } diff --git a/crates/fayalite/src/intern.rs b/crates/fayalite/src/intern.rs index c38f26f..3780ad3 100644 --- a/crates/fayalite/src/intern.rs +++ b/crates/fayalite/src/intern.rs @@ -1,10 +1,7 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // See Notices.txt for copyright information #![allow(clippy::type_complexity)] -use crate::{ - intern::type_map::TypeIdMap, - util::{ConstBool, GenericConstBool}, -}; +use crate::intern::type_map::TypeIdMap; use bitvec::{ptr::BitPtr, slice::BitSlice, vec::BitVec}; use hashbrown::{hash_map::RawEntryMut, HashMap, HashTable}; use serde::{Deserialize, Serialize}; @@ -297,221 +294,94 @@ impl InternedCompare for str { } } -pub trait InternContext: 'static + Send + Sync + Hash + Ord + fmt::Debug + Clone { - type InternedImpl: 'static + Send + Sync + Clone; - type InternedGuardImpl: 'static - + Send - + Sync - + Clone - + Borrow; - type AllContextsAreIdentical: GenericConstBool; - fn interned_compare_key( - v: &Self::InternedImpl, - ) -> T::InternedCompareKey { - T::interned_compare_key_ref(Self::guard(v).borrow()) +pub trait Intern: Any + Send + Sync { + fn intern(&self) -> Interned; + fn intern_sized(self) -> Interned + where + Self: Clone, + { + Self::intern_owned(self) } - fn guard( - v: &Self::InternedImpl, - ) -> Self::InternedGuardImpl; - fn try_guard( - v: &Self::InternedImpl, - ) -> Option>; - fn into_guard( - v: Self::InternedImpl, - ) -> Self::InternedGuardImpl { - Self::guard(&v) + fn intern_owned(this: ::Owned) -> Interned + where + Self: ToOwned, + { + Self::intern_cow(Cow::Owned(this)) } - fn unguard( - v: &Self::InternedGuardImpl, - ) -> Self::InternedImpl; - fn unguard_move( - v: Self::InternedGuardImpl, - ) -> Self::InternedImpl { - Self::unguard(&v) + fn intern_cow(this: Cow<'_, Self>) -> Interned + where + Self: ToOwned, + { + this.intern() } - fn alloc_str(&self, value: Cow<'_, str>) -> Self::InternedGuardImpl; - fn alloc_slice( - &self, - value: Cow<'_, [T]>, - ) -> Self::InternedGuardImpl<[T]>; - fn alloc_sized( - &self, - value: Cow<'_, T>, - ) -> Self::InternedGuardImpl; - fn interner(&self) -> &Interner; } -pub trait BitSliceInternContext: InternContext { - fn alloc_bit_slice(&self, value: Cow<'_, BitSlice>) -> Self::InternedGuardImpl; +pub struct Interner { + map: Mutex>, } -#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)] -pub struct GlobalContext; - -impl InternContext for GlobalContext { - type InternedImpl = &'static T; - type InternedGuardImpl = &'static T; - type AllContextsAreIdentical = ConstBool; - - fn guard( - v: &Self::InternedImpl, - ) -> Self::InternedGuardImpl { - *v - } - fn try_guard( - v: &Self::InternedImpl, - ) -> Option> { - Some(*v) - } - fn unguard( - v: &Self::InternedGuardImpl, - ) -> Self::InternedImpl { - *v - } - fn alloc_str(&self, value: Cow<'_, str>) -> Self::InternedGuardImpl { - value.into_owned().leak() - } - fn alloc_slice( - &self, - value: Cow<'_, [T]>, - ) -> Self::InternedGuardImpl<[T]> { - value.into_owned().leak() - } - fn alloc_sized( - &self, - value: Cow<'_, T>, - ) -> Self::InternedGuardImpl { - Box::leak(Box::new(value.into_owned())) - } - fn interner(&self) -> &Interner { +impl Interner { + fn get() -> &'static Interner { static TYPE_ID_MAP: TypeIdMap = TypeIdMap::new(); TYPE_ID_MAP.get_or_insert_default() } } -impl BitSliceInternContext for GlobalContext { - fn alloc_bit_slice(&self, value: Cow<'_, BitSlice>) -> Self::InternedGuardImpl { - value.into_owned().leak() - } -} - -pub trait Intern: Any + Send + Sync { - fn intern_with_ctx(&self, context: &C) -> Interned; - fn intern_sized_with_ctx(self, context: &C) -> Interned - where - Self: Clone, - { - Self::intern_owned_with_ctx(self, context) - } - fn intern_owned_with_ctx(this: ::Owned, context: &C) -> Interned - where - Self: ToOwned, - { - Self::intern_cow_with_ctx(Cow::Owned(this), context) - } - fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned - where - Self: ToOwned, - { - this.intern_with_ctx(context) - } - fn intern(&self) -> Interned - where - C: InternContext> + Default, - { - self.intern_with_ctx(&C::default()) - } - fn intern_sized(self) -> Interned - where - Self: Clone, - C: InternContext> + Default, - { - self.intern_sized_with_ctx(&C::default()) - } - fn intern_owned(this: ::Owned) -> Interned - where - Self: ToOwned, - C: InternContext> + Default, - { - Self::intern_owned_with_ctx(this, &C::default()) - } - fn intern_cow(this: Cow<'_, Self>) -> Interned - where - Self: ToOwned, - C: InternContext> + Default, - { - Self::intern_cow_with_ctx(this, &C::default()) - } -} - -pub struct Interner { - map: Mutex, ()>>, - _phantom: PhantomData, -} - -impl Default for Interner { +impl Default for Interner { fn default() -> Self { Self { map: Default::default(), - _phantom: Default::default(), } } } -impl Interner { - fn intern) -> C::InternedGuardImpl>( +impl Interner { + fn intern) -> &'static T>( &self, alloc: F, value: Cow<'_, T>, - ) -> Interned { + ) -> Interned { let mut map = self.map.lock().unwrap(); let hasher = map.hasher().clone(); let hash = hasher.hash_one(&*value); - let inner = match map - .raw_entry_mut() - .from_hash(hash, |k| k.borrow() == &*value) - { - RawEntryMut::Occupied(entry) => C::unguard(entry.key()), - RawEntryMut::Vacant(entry) => C::unguard( - entry - .insert_with_hasher(hash, alloc(value), (), |k| hasher.hash_one(k.borrow())) - .0, - ), + let inner = match map.raw_entry_mut().from_hash(hash, |k| **k == *value) { + RawEntryMut::Occupied(entry) => *entry.key(), + RawEntryMut::Vacant(entry) => { + *entry + .insert_with_hasher(hash, alloc(value), (), |k| hasher.hash_one(&**k)) + .0 + } }; - Interned { - inner, - _phantom: PhantomData, - } + Interned { inner } } } -impl Interner { - fn intern_sized(&self, context: &C, value: Cow<'_, T>) -> Interned { - self.intern(|value| context.alloc_sized(value), value) +impl Interner { + fn intern_sized(&self, value: Cow<'_, T>) -> Interned { + self.intern(|value| Box::leak(Box::new(value.into_owned())), value) } } -impl Interner<[T], C> { - fn intern_slice(&self, context: &C, value: Cow<'_, [T]>) -> Interned<[T], C> { - self.intern(|value| context.alloc_slice(value), value) +impl Interner<[T]> { + fn intern_slice(&self, value: Cow<'_, [T]>) -> Interned<[T]> { + self.intern(|value| value.into_owned().leak(), value) } } -impl Interner { - fn intern_bit_slice(&self, context: &C, value: Cow<'_, BitSlice>) -> Interned { - self.intern(|value| context.alloc_bit_slice(value), value) +impl Interner { + fn intern_bit_slice(&self, value: Cow<'_, BitSlice>) -> Interned { + self.intern(|value| value.into_owned().leak(), value) } } -impl Interner { - fn intern_str(&self, context: &C, value: Cow<'_, str>) -> Interned { - self.intern(|value| context.alloc_str(value), value) +impl Interner { + fn intern_str(&self, value: Cow<'_, str>) -> Interned { + self.intern(|value| value.into_owned().leak(), value) } } -pub struct Interned { - inner: C::InternedImpl, - _phantom: PhantomData<&'static C>, +pub struct Interned { + inner: &'static T, } macro_rules! forward_fmt_trait { @@ -522,23 +392,9 @@ macro_rules! forward_fmt_trait { } } - impl fmt::$Tr - for Interned - { + impl fmt::$Tr for Interned { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let Some(guard) = C::try_guard(&self.inner) { - guard.borrow().fmt(f) - } else { - write!(f, "") - } - } - } - - impl fmt::$Tr - for Guard - { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.inner.borrow().fmt(f) + self.inner.fmt(f) } } }; @@ -554,18 +410,16 @@ forward_fmt_trait!(UpperExp); forward_fmt_trait!(UpperHex); #[derive(Clone, Debug)] -pub struct InternedSliceIter { - slice: Interned<[T], C>, +pub struct InternedSliceIter { + slice: Interned<[T]>, index: std::ops::Range, } -impl Iterator for InternedSliceIter { +impl Iterator for InternedSliceIter { type Item = T; fn next(&mut self) -> Option { - self.index - .next() - .map(|index| self.slice.guard()[index].clone()) + self.index.next().map(|index| self.slice[index].clone()) } fn size_hint(&self) -> (usize, Option) { @@ -573,233 +427,186 @@ impl Iterator for InternedSl } } -impl DoubleEndedIterator - for InternedSliceIter -{ +impl DoubleEndedIterator for InternedSliceIter { fn next_back(&mut self) -> Option { self.index .next_back() - .map(|index| self.slice.guard()[index].clone()) + .map(|index| self.slice[index].clone()) } } -impl FusedIterator for InternedSliceIter {} +impl FusedIterator for InternedSliceIter {} -impl ExactSizeIterator - for InternedSliceIter -{ -} +impl ExactSizeIterator for InternedSliceIter {} -impl IntoIterator for Interned<[T], C> { +impl IntoIterator for Interned<[T]> { type Item = T; - type IntoIter = InternedSliceIter; + type IntoIter = InternedSliceIter; fn into_iter(self) -> Self::IntoIter { InternedSliceIter { - index: 0..self.guard().len(), + index: 0..self.len(), slice: self, } } } -impl<'a, T: 'static + Send + Sync, C: InternContext> IntoIterator for &'a Interned<[T], C> -where - C::InternedImpl<[T]>: Borrow<[T]>, -{ +impl<'a, T: 'static + Send + Sync> IntoIterator for &'a Interned<[T]> { type Item = &'a T; type IntoIter = std::slice::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { - self.inner.borrow().iter() + self.inner.iter() } } -impl<'a, T: 'static + Send + Sync, C: InternContext> IntoIterator for &'a mut Interned<[T], C> -where - C::InternedImpl<[T]>: Borrow<[T]>, -{ +impl<'a, T: 'static + Send + Sync> IntoIterator for &'a mut Interned<[T]> { type Item = &'a T; type IntoIter = std::slice::Iter<'a, T>; fn into_iter(self) -> Self::IntoIter { - self.inner.borrow().iter() + self.inner.iter() } } -impl> + Default> - FromIterator for Interned<[I], C> +impl FromIterator for Interned<[I]> where - [I]: Intern, + [I]: Intern, { fn from_iter>(iter: T) -> Self { Intern::intern_owned(Vec::from_iter(iter)) } } -impl From> for Vec { - fn from(value: Interned<[T], C>) -> Self { - Vec::from(&*value.guard()) +impl From> for Vec { + fn from(value: Interned<[T]>) -> Self { + Vec::from(&*value) } } -impl From> for Box<[T]> { - fn from(value: Interned<[T], C>) -> Self { - Box::from(&*value.guard()) +impl From> for Box<[T]> { + fn from(value: Interned<[T]>) -> Self { + Box::from(&*value) } } -impl From> for String { - fn from(value: Interned) -> Self { - String::from(&*value.guard()) +impl From> for String { + fn from(value: Interned) -> Self { + String::from(&*value) } } -impl> + Default> Default - for Interned<[I], C> +impl Default for Interned<[I]> where - [I]: Intern, + [I]: Intern, { fn default() -> Self { [][..].intern() } } -impl> + Default> Default - for Interned -where - str: Intern, -{ +impl Default for Interned { fn default() -> Self { "".intern() } } -impl> + Default> Default - for Interned -where - BitSlice: Intern, -{ +impl Default for Interned { fn default() -> Self { <&BitSlice>::default().intern() } } -impl> + Default> - Default for Interned +impl Default for Interned where - I: Intern, + I: Intern, { fn default() -> Self { I::default().intern() } } -impl Interned { +impl Interned { pub fn cast_unchecked( this: Self, - f: impl FnOnce(C::InternedImpl) -> C::InternedImpl, - ) -> Interned { + f: impl FnOnce(&'static T) -> &'static U, + ) -> Interned { Interned { inner: f(this.inner), - _phantom: PhantomData, } } pub fn try_cast_unchecked( this: Self, - f: impl FnOnce(C::InternedImpl) -> Result, E>, - ) -> Result, E> { + f: impl FnOnce(&'static T) -> Result<&'static U, E>, + ) -> Result, E> { Ok(Interned { inner: f(this.inner)?, - _phantom: PhantomData, }) } - pub fn into_inner(this: Self) -> C::InternedImpl { + pub fn into_inner(this: Self) -> &'static T { this.inner } - pub fn get_ref(this: &Self) -> &C::InternedImpl { + pub fn get_ref(this: &Self) -> &&'static T { &this.inner } - pub fn guard(&self) -> Guard { - Guard { - inner: C::guard(&self.inner), - _phantom: PhantomData, - } - } } -impl Clone for Interned { +impl Clone for Interned { fn clone(&self) -> Self { - Interned { - inner: self.inner.clone(), - _phantom: PhantomData, - } + *self } } -impl Copy for Interned where - C::InternedImpl: Copy -{ -} +impl Copy for Interned where &'static T: Copy {} -impl Deref for Interned +impl Deref for Interned where - C::InternedImpl: Borrow, + &'static T: Borrow, { type Target = T; fn deref(&self) -> &Self::Target { - self.inner.borrow() + self.inner } } -impl PartialEq - for Interned -{ +impl PartialEq for Interned { fn eq(&self, other: &Self) -> bool { - ::interned_compare_key(&self.inner) - == ::interned_compare_key(&other.inner) + T::interned_compare_key_ref(self.inner) == T::interned_compare_key_ref(other.inner) } } -impl Eq for Interned {} +impl Eq for Interned {} -impl PartialOrd - for Interned -{ +impl PartialOrd for Interned { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for Interned { +impl Ord for Interned { fn cmp(&self, other: &Self) -> Ordering { - ::interned_compare_key(&self.inner) - .cmp(&::interned_compare_key(&other.inner)) + T::interned_compare_key_ref(self.inner).cmp(&T::interned_compare_key_ref(other.inner)) } } -impl Hash - for Interned -{ +impl Hash for Interned { fn hash(&self, state: &mut H) { - ::interned_compare_key(&self.inner).hash(state); + T::interned_compare_key_ref(self.inner).hash(state); } } -impl Serialize for Interned { +impl Serialize for Interned { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { - self.guard().serialize(serializer) + T::serialize(self, serializer) } } -impl< - 'de, - T: 'static + Send + Sync + Deserialize<'de> + Clone + Intern, - C: InternContext> + Default, - > Deserialize<'de> for Interned +impl<'de, T: 'static + Send + Sync + Deserialize<'de> + Clone + Intern> Deserialize<'de> + for Interned { fn deserialize(deserializer: D) -> Result where @@ -809,13 +616,9 @@ impl< } } -impl< - 'de, - T: 'static + Send + Sync + Clone, - C: InternContext> + Default, - > Deserialize<'de> for Interned<[T], C> +impl<'de, T: 'static + Send + Sync + Clone> Deserialize<'de> for Interned<[T]> where - [T]: Intern, + [T]: Intern, Vec: Deserialize<'de>, { fn deserialize(deserializer: D) -> Result @@ -826,11 +629,7 @@ where } } -impl<'de, C: BitSliceInternContext> + Default> - Deserialize<'de> for Interned -where - BitSlice: Intern, -{ +impl<'de> Deserialize<'de> for Interned { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -839,11 +638,7 @@ where } } -impl<'de, C: InternContext> + Default> Deserialize<'de> - for Interned -where - str: Intern, -{ +impl<'de> Deserialize<'de> for Interned { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de>, @@ -852,175 +647,83 @@ where } } -pub struct Guard { - inner: C::InternedGuardImpl, - _phantom: PhantomData<&'static C>, -} - -impl Guard { - pub fn cast_unchecked( - this: Self, - f: impl FnOnce(C::InternedGuardImpl) -> C::InternedGuardImpl, - ) -> Guard { - Guard { - inner: f(this.inner), - _phantom: PhantomData, - } - } - pub fn try_cast_unchecked( - this: Self, - f: impl FnOnce(C::InternedGuardImpl) -> Result, E>, - ) -> Result, E> { - Ok(Guard { - inner: f(this.inner)?, - _phantom: PhantomData, - }) - } - pub fn into_inner(this: Self) -> C::InternedGuardImpl { - this.inner - } - pub fn get_ref(this: &Self) -> &C::InternedGuardImpl { - &this.inner - } - pub fn unguard(&self) -> Interned { - Interned { - inner: C::unguard(&self.inner), - _phantom: PhantomData, - } - } -} - -impl Clone for Guard { - fn clone(&self) -> Self { - Guard { - inner: self.inner.clone(), - _phantom: PhantomData, - } - } -} - -impl Copy for Guard where - C::InternedGuardImpl: Copy -{ -} - -impl Deref for Guard { - type Target = T; - - fn deref(&self) -> &Self::Target { - self.inner.borrow() - } -} - -impl PartialEq - for Guard -{ - fn eq(&self, other: &Self) -> bool { - T::interned_compare_key_ref(self.inner.borrow()) - == T::interned_compare_key_ref(other.inner.borrow()) - } -} - -impl Eq for Guard {} - -impl PartialOrd - for Guard -{ - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) - } -} - -impl Ord for Guard { - fn cmp(&self, other: &Self) -> Ordering { - T::interned_compare_key_ref(self.inner.borrow()) - .cmp(&T::interned_compare_key_ref(other.inner.borrow())) - } -} - -impl Hash for Guard { - fn hash(&self, state: &mut H) { - T::interned_compare_key_ref(self.inner.borrow()).hash(state); - } -} - -impl Intern for T { - fn intern_with_ctx(&self, context: &C) -> Interned { - Self::intern_cow_with_ctx(Cow::Borrowed(self), context) +impl Intern for T { + fn intern(&self) -> Interned { + Self::intern_cow(Cow::Borrowed(self)) } - fn intern_owned_with_ctx(this: ::Owned, context: &C) -> Interned + fn intern_owned(this: ::Owned) -> Interned where Self: ToOwned, { - Self::intern_cow_with_ctx(Cow::Owned(this), context) + Self::intern_cow(Cow::Owned(this)) } - fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned + fn intern_cow(this: Cow<'_, Self>) -> Interned where Self: ToOwned, { - context.interner().intern_sized(context, this) + Interner::get().intern_sized(this) } } -impl Intern for [T] { - fn intern_with_ctx(&self, context: &C) -> Interned { - Self::intern_cow_with_ctx(Cow::Borrowed(self), context) +impl Intern for [T] { + fn intern(&self) -> Interned { + Self::intern_cow(Cow::Borrowed(self)) } - fn intern_owned_with_ctx(this: ::Owned, context: &C) -> Interned + fn intern_owned(this: ::Owned) -> Interned where Self: ToOwned, { - Self::intern_cow_with_ctx(Cow::Owned(this), context) + Self::intern_cow(Cow::Owned(this)) } - fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned + fn intern_cow(this: Cow<'_, Self>) -> Interned where Self: ToOwned, { - context.interner().intern_slice(context, this) + Interner::get().intern_slice(this) } } -impl Intern for BitSlice { - fn intern_with_ctx(&self, context: &C) -> Interned { - Self::intern_cow_with_ctx(Cow::Borrowed(self), context) +impl Intern for BitSlice { + fn intern(&self) -> Interned { + Self::intern_cow(Cow::Borrowed(self)) } - fn intern_owned_with_ctx(this: ::Owned, context: &C) -> Interned + fn intern_owned(this: ::Owned) -> Interned where Self: ToOwned, { - Self::intern_cow_with_ctx(Cow::Owned(this), context) + Self::intern_cow(Cow::Owned(this)) } - fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned + fn intern_cow(this: Cow<'_, Self>) -> Interned where Self: ToOwned, { - context.interner().intern_bit_slice(context, this) + Interner::get().intern_bit_slice(this) } } -impl Intern for str { - fn intern_with_ctx(&self, context: &C) -> Interned { - Self::intern_cow_with_ctx(Cow::Borrowed(self), context) +impl Intern for str { + fn intern(&self) -> Interned { + Self::intern_cow(Cow::Borrowed(self)) } - fn intern_owned_with_ctx(this: ::Owned, context: &C) -> Interned + fn intern_owned(this: ::Owned) -> Interned where Self: ToOwned, { - Self::intern_cow_with_ctx(Cow::Owned(this), context) + Self::intern_cow(Cow::Owned(this)) } - fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned + fn intern_cow(this: Cow<'_, Self>) -> Interned where Self: ToOwned, { - context.interner().intern_str(context, this) + Interner::get().intern_str(this) } } diff --git a/crates/fayalite/src/ty.rs b/crates/fayalite/src/ty.rs index 380d2e6..69080c9 100644 --- a/crates/fayalite/src/ty.rs +++ b/crates/fayalite/src/ty.rs @@ -330,6 +330,6 @@ impl Index for AsMaskWithoutGenerics { type Output = T::MaskType; fn index(&self, ty: T) -> &Self::Output { - Interned::<_>::into_inner(Intern::intern_sized(ty.mask_type())) + Interned::into_inner(Intern::intern_sized(ty.mask_type())) } }