remove interning contexts

This commit is contained in:
Jacob Lifshay 2024-10-07 21:33:56 -07:00
parent 44ca1a607a
commit f12322aa2a
Signed by: programmerjake
SSH key fingerprint: SHA256:B1iRVvUJkvd7upMIiMqn6OyxvD2SgJkAH3ZnUOj6z+c
8 changed files with 154 additions and 451 deletions

View file

@ -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))
}
}
}

View file

@ -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),
)
}

View file

@ -187,7 +187,7 @@ impl<T: Type, Len: Size> TypeWithDeref for ArrayType<T, Len> {
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::<T>::try_from(retval)
.ok()
.expect("unreachable"),
@ -202,7 +202,7 @@ impl<T: Type> Index<T> for ArrayWithoutGenerics {
type Output = ArrayWithoutLen<T>;
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<T: Type, L: SizeType> Index<L> for ArrayWithoutLen<T> {
type Output = ArrayType<T, L::Size>;
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)))
}
}

View file

@ -377,7 +377,7 @@ macro_rules! impl_tuples {
impl<$($T: Type,)*> TypeWithDeref for ($($T,)*) {
fn expr_deref(this: &Expr<Self>) -> &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,)*) {

View file

@ -2057,7 +2057,7 @@ impl<ElementType: Type, Len: Size> ExprIndex<usize> for ArrayType<ElementType, L
#[track_caller]
fn expr_index(this: &Expr<Self>, index: usize) -> &Expr<Self::Output> {
Interned::<_>::into_inner(
Interned::into_inner(
ArrayIndex::<ElementType>::new(Expr::as_dyn_array(*this), index)
.to_expr()
.intern_sized(),
@ -2154,7 +2154,7 @@ impl<ElementType: Type, Len: Size, Width: Size> ExprIndex<Expr<UIntType<Width>>>
type Output = ElementType;
fn expr_index(this: &Expr<Self>, index: Expr<UIntType<Width>>) -> &Expr<Self::Output> {
Interned::<_>::into_inner(
Interned::into_inner(
DynArrayIndex::<ElementType>::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::<Bool>()

View file

@ -301,7 +301,7 @@ macro_rules! impl_int {
type Output = $name<Width::Size>;
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)))
}
}

View file

@ -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<T: ?Sized + 'static + Send + Sync>: 'static + Send + Sync + Clone;
type InternedGuardImpl<T: ?Sized + 'static + Send + Sync>: 'static
+ Send
+ Sync
+ Clone
+ Borrow<T>;
type AllContextsAreIdentical: GenericConstBool;
fn interned_compare_key<T: InternedCompare + ?Sized + 'static + Send + Sync>(
v: &Self::InternedImpl<T>,
) -> T::InternedCompareKey {
T::interned_compare_key_ref(Self::guard(v).borrow())
pub trait Intern: Any + Send + Sync {
fn intern(&self) -> Interned<Self>;
fn intern_sized(self) -> Interned<Self>
where
Self: Clone,
{
Self::intern_owned(self)
}
fn guard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedImpl<T>,
) -> Self::InternedGuardImpl<T>;
fn try_guard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedImpl<T>,
) -> Option<Self::InternedGuardImpl<T>>;
fn into_guard<T: ?Sized + 'static + Send + Sync>(
v: Self::InternedImpl<T>,
) -> Self::InternedGuardImpl<T> {
Self::guard(&v)
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self>
where
Self: ToOwned,
{
Self::intern_cow(Cow::Owned(this))
}
fn unguard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedGuardImpl<T>,
) -> Self::InternedImpl<T>;
fn unguard_move<T: ?Sized + 'static + Send + Sync>(
v: Self::InternedGuardImpl<T>,
) -> Self::InternedImpl<T> {
Self::unguard(&v)
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self>
where
Self: ToOwned,
{
this.intern()
}
fn alloc_str(&self, value: Cow<'_, str>) -> Self::InternedGuardImpl<str>;
fn alloc_slice<T: Clone + Send + Sync + 'static>(
&self,
value: Cow<'_, [T]>,
) -> Self::InternedGuardImpl<[T]>;
fn alloc_sized<T: Clone + Send + Sync + 'static>(
&self,
value: Cow<'_, T>,
) -> Self::InternedGuardImpl<T>;
fn interner<T: ?Sized + Send + Sync + 'static>(&self) -> &Interner<T, Self>;
}
pub trait BitSliceInternContext: InternContext {
fn alloc_bit_slice(&self, value: Cow<'_, BitSlice>) -> Self::InternedGuardImpl<BitSlice>;
pub struct Interner<T: ?Sized + 'static + Send + Sync> {
map: Mutex<HashMap<&'static T, ()>>,
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
pub struct GlobalContext;
impl InternContext for GlobalContext {
type InternedImpl<T: ?Sized + 'static + Send + Sync> = &'static T;
type InternedGuardImpl<T: ?Sized + 'static + Send + Sync> = &'static T;
type AllContextsAreIdentical = ConstBool<true>;
fn guard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedImpl<T>,
) -> Self::InternedGuardImpl<T> {
*v
}
fn try_guard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedImpl<T>,
) -> Option<Self::InternedGuardImpl<T>> {
Some(*v)
}
fn unguard<T: ?Sized + 'static + Send + Sync>(
v: &Self::InternedGuardImpl<T>,
) -> Self::InternedImpl<T> {
*v
}
fn alloc_str(&self, value: Cow<'_, str>) -> Self::InternedGuardImpl<str> {
value.into_owned().leak()
}
fn alloc_slice<T: Clone + Send + Sync + 'static>(
&self,
value: Cow<'_, [T]>,
) -> Self::InternedGuardImpl<[T]> {
value.into_owned().leak()
}
fn alloc_sized<T: Clone + Send + Sync + 'static>(
&self,
value: Cow<'_, T>,
) -> Self::InternedGuardImpl<T> {
Box::leak(Box::new(value.into_owned()))
}
fn interner<T: ?Sized + 'static + Send + Sync>(&self) -> &Interner<T, Self> {
impl<T: ?Sized + 'static + Send + Sync> Interner<T> {
fn get() -> &'static Interner<T> {
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<BitSlice> {
value.into_owned().leak()
}
}
pub trait Intern<C: InternContext = GlobalContext>: Any + Send + Sync {
fn intern_with_ctx(&self, context: &C) -> Interned<Self, C>;
fn intern_sized_with_ctx(self, context: &C) -> Interned<Self, C>
where
Self: Clone,
{
Self::intern_owned_with_ctx(self, context)
}
fn intern_owned_with_ctx(this: <Self as ToOwned>::Owned, context: &C) -> Interned<Self, C>
where
Self: ToOwned,
{
Self::intern_cow_with_ctx(Cow::Owned(this), context)
}
fn intern_cow_with_ctx(this: Cow<'_, Self>, context: &C) -> Interned<Self, C>
where
Self: ToOwned,
{
this.intern_with_ctx(context)
}
fn intern(&self) -> Interned<Self, C>
where
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
{
self.intern_with_ctx(&C::default())
}
fn intern_sized(self) -> Interned<Self, C>
where
Self: Clone,
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
{
self.intern_sized_with_ctx(&C::default())
}
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self, C>
where
Self: ToOwned,
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
{
Self::intern_owned_with_ctx(this, &C::default())
}
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self, C>
where
Self: ToOwned,
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
{
Self::intern_cow_with_ctx(this, &C::default())
}
}
pub struct Interner<T: ?Sized + 'static + Send + Sync, C: InternContext> {
map: Mutex<HashMap<C::InternedGuardImpl<T>, ()>>,
_phantom: PhantomData<fn(&C)>,
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Default for Interner<T, C> {
impl<T: ?Sized + 'static + Send + Sync> Default for Interner<T> {
fn default() -> Self {
Self {
map: Default::default(),
_phantom: Default::default(),
}
}
}
impl<T: ?Sized + 'static + Send + Sync + Hash + Eq + ToOwned, C: InternContext> Interner<T, C> {
fn intern<F: FnOnce(Cow<'_, T>) -> C::InternedGuardImpl<T>>(
impl<T: ?Sized + 'static + Send + Sync + Hash + Eq + ToOwned> Interner<T> {
fn intern<F: FnOnce(Cow<'_, T>) -> &'static T>(
&self,
alloc: F,
value: Cow<'_, T>,
) -> Interned<T, C> {
) -> Interned<T> {
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<T: Clone + 'static + Send + Sync + Hash + Eq, C: InternContext> Interner<T, C> {
fn intern_sized(&self, context: &C, value: Cow<'_, T>) -> Interned<T, C> {
self.intern(|value| context.alloc_sized(value), value)
impl<T: Clone + 'static + Send + Sync + Hash + Eq> Interner<T> {
fn intern_sized(&self, value: Cow<'_, T>) -> Interned<T> {
self.intern(|value| Box::leak(Box::new(value.into_owned())), value)
}
}
impl<T: Clone + 'static + Send + Sync + Hash + Eq, C: InternContext> Interner<[T], C> {
fn intern_slice(&self, context: &C, value: Cow<'_, [T]>) -> Interned<[T], C> {
self.intern(|value| context.alloc_slice(value), value)
impl<T: Clone + 'static + Send + Sync + Hash + Eq> Interner<[T]> {
fn intern_slice(&self, value: Cow<'_, [T]>) -> Interned<[T]> {
self.intern(|value| value.into_owned().leak(), value)
}
}
impl<C: BitSliceInternContext> Interner<BitSlice, C> {
fn intern_bit_slice(&self, context: &C, value: Cow<'_, BitSlice>) -> Interned<BitSlice, C> {
self.intern(|value| context.alloc_bit_slice(value), value)
impl Interner<BitSlice> {
fn intern_bit_slice(&self, value: Cow<'_, BitSlice>) -> Interned<BitSlice> {
self.intern(|value| value.into_owned().leak(), value)
}
}
impl<C: InternContext> Interner<str, C> {
fn intern_str(&self, context: &C, value: Cow<'_, str>) -> Interned<str, C> {
self.intern(|value| context.alloc_str(value), value)
impl Interner<str> {
fn intern_str(&self, value: Cow<'_, str>) -> Interned<str> {
self.intern(|value| value.into_owned().leak(), value)
}
}
pub struct Interned<T: ?Sized + 'static + Send + Sync, C: InternContext = GlobalContext> {
inner: C::InternedImpl<T>,
_phantom: PhantomData<&'static C>,
pub struct Interned<T: ?Sized + 'static + Send + Sync> {
inner: &'static T,
}
macro_rules! forward_fmt_trait {
@ -522,23 +392,9 @@ macro_rules! forward_fmt_trait {
}
}
impl<T: ?Sized + 'static + Send + Sync + fmt::$Tr, C: InternContext> fmt::$Tr
for Interned<T, C>
{
impl<T: ?Sized + 'static + Send + Sync + fmt::$Tr> fmt::$Tr for Interned<T> {
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, "<Expired>")
}
}
}
impl<T: ?Sized + 'static + Send + Sync + fmt::$Tr, C: InternContext> fmt::$Tr
for Guard<T, C>
{
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<T: Clone + 'static + Send + Sync, C: InternContext> {
slice: Interned<[T], C>,
pub struct InternedSliceIter<T: Clone + 'static + Send + Sync> {
slice: Interned<[T]>,
index: std::ops::Range<usize>,
}
impl<T: Clone + 'static + Send + Sync, C: InternContext> Iterator for InternedSliceIter<T, C> {
impl<T: Clone + 'static + Send + Sync> Iterator for InternedSliceIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
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<usize>) {
@ -573,233 +427,186 @@ impl<T: Clone + 'static + Send + Sync, C: InternContext> Iterator for InternedSl
}
}
impl<T: Clone + 'static + Send + Sync, C: InternContext> DoubleEndedIterator
for InternedSliceIter<T, C>
{
impl<T: Clone + 'static + Send + Sync> DoubleEndedIterator for InternedSliceIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.index
.next_back()
.map(|index| self.slice.guard()[index].clone())
.map(|index| self.slice[index].clone())
}
}
impl<T: Clone + 'static + Send + Sync, C: InternContext> FusedIterator for InternedSliceIter<T, C> {}
impl<T: Clone + 'static + Send + Sync> FusedIterator for InternedSliceIter<T> {}
impl<T: Clone + 'static + Send + Sync, C: InternContext> ExactSizeIterator
for InternedSliceIter<T, C>
{
}
impl<T: Clone + 'static + Send + Sync> ExactSizeIterator for InternedSliceIter<T> {}
impl<T: Clone + 'static + Send + Sync, C: InternContext> IntoIterator for Interned<[T], C> {
impl<T: Clone + 'static + Send + Sync> IntoIterator for Interned<[T]> {
type Item = T;
type IntoIter = InternedSliceIter<T, C>;
type IntoIter = InternedSliceIter<T>;
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<I: Clone, C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default>
FromIterator<I> for Interned<[I], C>
impl<I: Clone> FromIterator<I> for Interned<[I]>
where
[I]: Intern<C>,
[I]: Intern,
{
fn from_iter<T: IntoIterator<Item = I>>(iter: T) -> Self {
Intern::intern_owned(Vec::from_iter(iter))
}
}
impl<T: 'static + Clone + Send + Sync, C: InternContext> From<Interned<[T], C>> for Vec<T> {
fn from(value: Interned<[T], C>) -> Self {
Vec::from(&*value.guard())
impl<T: 'static + Clone + Send + Sync> From<Interned<[T]>> for Vec<T> {
fn from(value: Interned<[T]>) -> Self {
Vec::from(&*value)
}
}
impl<T: 'static + Clone + Send + Sync, C: InternContext> From<Interned<[T], C>> for Box<[T]> {
fn from(value: Interned<[T], C>) -> Self {
Box::from(&*value.guard())
impl<T: 'static + Clone + Send + Sync> From<Interned<[T]>> for Box<[T]> {
fn from(value: Interned<[T]>) -> Self {
Box::from(&*value)
}
}
impl<C: InternContext> From<Interned<str, C>> for String {
fn from(value: Interned<str, C>) -> Self {
String::from(&*value.guard())
impl From<Interned<str>> for String {
fn from(value: Interned<str>) -> Self {
String::from(&*value)
}
}
impl<I, C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default> Default
for Interned<[I], C>
impl<I> Default for Interned<[I]>
where
[I]: Intern<C>,
[I]: Intern,
{
fn default() -> Self {
[][..].intern()
}
}
impl<C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default> Default
for Interned<str, C>
where
str: Intern<C>,
{
impl Default for Interned<str> {
fn default() -> Self {
"".intern()
}
}
impl<C: BitSliceInternContext<AllContextsAreIdentical = ConstBool<true>> + Default> Default
for Interned<BitSlice, C>
where
BitSlice: Intern<C>,
{
impl Default for Interned<BitSlice> {
fn default() -> Self {
<&BitSlice>::default().intern()
}
}
impl<I: Clone + Default, C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default>
Default for Interned<I, C>
impl<I: Clone + Default> Default for Interned<I>
where
I: Intern<C>,
I: Intern,
{
fn default() -> Self {
I::default().intern()
}
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Interned<T, C> {
impl<T: ?Sized + 'static + Send + Sync> Interned<T> {
pub fn cast_unchecked<U: ?Sized + 'static + Send + Sync>(
this: Self,
f: impl FnOnce(C::InternedImpl<T>) -> C::InternedImpl<U>,
) -> Interned<U, C> {
f: impl FnOnce(&'static T) -> &'static U,
) -> Interned<U> {
Interned {
inner: f(this.inner),
_phantom: PhantomData,
}
}
pub fn try_cast_unchecked<U: ?Sized + 'static + Send + Sync, E>(
this: Self,
f: impl FnOnce(C::InternedImpl<T>) -> Result<C::InternedImpl<U>, E>,
) -> Result<Interned<U, C>, E> {
f: impl FnOnce(&'static T) -> Result<&'static U, E>,
) -> Result<Interned<U>, E> {
Ok(Interned {
inner: f(this.inner)?,
_phantom: PhantomData,
})
}
pub fn into_inner(this: Self) -> C::InternedImpl<T> {
pub fn into_inner(this: Self) -> &'static T {
this.inner
}
pub fn get_ref(this: &Self) -> &C::InternedImpl<T> {
pub fn get_ref(this: &Self) -> &&'static T {
&this.inner
}
pub fn guard(&self) -> Guard<T, C> {
Guard {
inner: C::guard(&self.inner),
_phantom: PhantomData,
}
}
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Clone for Interned<T, C> {
impl<T: ?Sized + 'static + Send + Sync> Clone for Interned<T> {
fn clone(&self) -> Self {
Interned {
inner: self.inner.clone(),
_phantom: PhantomData,
}
*self
}
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Copy for Interned<T, C> where
C::InternedImpl<T>: Copy
{
}
impl<T: ?Sized + 'static + Send + Sync> Copy for Interned<T> where &'static T: Copy {}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Deref for Interned<T, C>
impl<T: ?Sized + 'static + Send + Sync> Deref for Interned<T>
where
C::InternedImpl<T>: Borrow<T>,
&'static T: Borrow<T>,
{
type Target = T;
fn deref(&self) -> &Self::Target {
self.inner.borrow()
self.inner
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> PartialEq
for Interned<T, C>
{
impl<T: ?Sized + 'static + Send + Sync + InternedCompare> PartialEq for Interned<T> {
fn eq(&self, other: &Self) -> bool {
<C as InternContext>::interned_compare_key(&self.inner)
== <C as InternContext>::interned_compare_key(&other.inner)
T::interned_compare_key_ref(self.inner) == T::interned_compare_key_ref(other.inner)
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Eq for Interned<T, C> {}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare> Eq for Interned<T> {}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> PartialOrd
for Interned<T, C>
{
impl<T: ?Sized + 'static + Send + Sync + InternedCompare> PartialOrd for Interned<T> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Ord for Interned<T, C> {
impl<T: ?Sized + 'static + Send + Sync + InternedCompare> Ord for Interned<T> {
fn cmp(&self, other: &Self) -> Ordering {
<C as InternContext>::interned_compare_key(&self.inner)
.cmp(&<C as InternContext>::interned_compare_key(&other.inner))
T::interned_compare_key_ref(self.inner).cmp(&T::interned_compare_key_ref(other.inner))
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Hash
for Interned<T, C>
{
impl<T: ?Sized + 'static + Send + Sync + InternedCompare> Hash for Interned<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
<C as InternContext>::interned_compare_key(&self.inner).hash(state);
T::interned_compare_key_ref(self.inner).hash(state);
}
}
impl<T: ?Sized + 'static + Send + Sync + Serialize, C: InternContext> Serialize for Interned<T, C> {
impl<T: ?Sized + 'static + Send + Sync + Serialize> Serialize for Interned<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.guard().serialize(serializer)
T::serialize(self, serializer)
}
}
impl<
'de,
T: 'static + Send + Sync + Deserialize<'de> + Clone + Intern<C>,
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
> Deserialize<'de> for Interned<T, C>
impl<'de, T: 'static + Send + Sync + Deserialize<'de> + Clone + Intern> Deserialize<'de>
for Interned<T>
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@ -809,13 +616,9 @@ impl<
}
}
impl<
'de,
T: 'static + Send + Sync + Clone,
C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default,
> Deserialize<'de> for Interned<[T], C>
impl<'de, T: 'static + Send + Sync + Clone> Deserialize<'de> for Interned<[T]>
where
[T]: Intern<C>,
[T]: Intern,
Vec<T>: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
@ -826,11 +629,7 @@ where
}
}
impl<'de, C: BitSliceInternContext<AllContextsAreIdentical = ConstBool<true>> + Default>
Deserialize<'de> for Interned<BitSlice, C>
where
BitSlice: Intern<C>,
{
impl<'de> Deserialize<'de> for Interned<BitSlice> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
@ -839,11 +638,7 @@ where
}
}
impl<'de, C: InternContext<AllContextsAreIdentical = ConstBool<true>> + Default> Deserialize<'de>
for Interned<str, C>
where
str: Intern<C>,
{
impl<'de> Deserialize<'de> for Interned<str> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
@ -852,175 +647,83 @@ where
}
}
pub struct Guard<T: ?Sized + 'static + Send + Sync, C: InternContext = GlobalContext> {
inner: C::InternedGuardImpl<T>,
_phantom: PhantomData<&'static C>,
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Guard<T, C> {
pub fn cast_unchecked<U: ?Sized + 'static + Send + Sync>(
this: Self,
f: impl FnOnce(C::InternedGuardImpl<T>) -> C::InternedGuardImpl<U>,
) -> Guard<U, C> {
Guard {
inner: f(this.inner),
_phantom: PhantomData,
}
}
pub fn try_cast_unchecked<U: ?Sized + 'static + Send + Sync, E>(
this: Self,
f: impl FnOnce(C::InternedGuardImpl<T>) -> Result<C::InternedGuardImpl<U>, E>,
) -> Result<Guard<U, C>, E> {
Ok(Guard {
inner: f(this.inner)?,
_phantom: PhantomData,
})
}
pub fn into_inner(this: Self) -> C::InternedGuardImpl<T> {
this.inner
}
pub fn get_ref(this: &Self) -> &C::InternedGuardImpl<T> {
&this.inner
}
pub fn unguard(&self) -> Interned<T, C> {
Interned {
inner: C::unguard(&self.inner),
_phantom: PhantomData,
}
}
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Clone for Guard<T, C> {
fn clone(&self) -> Self {
Guard {
inner: self.inner.clone(),
_phantom: PhantomData,
}
}
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Copy for Guard<T, C> where
C::InternedGuardImpl<T>: Copy
{
}
impl<T: ?Sized + 'static + Send + Sync, C: InternContext> Deref for Guard<T, C> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.inner.borrow()
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> PartialEq
for Guard<T, C>
{
fn eq(&self, other: &Self) -> bool {
T::interned_compare_key_ref(self.inner.borrow())
== T::interned_compare_key_ref(other.inner.borrow())
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Eq for Guard<T, C> {}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> PartialOrd
for Guard<T, C>
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Ord for Guard<T, C> {
fn cmp(&self, other: &Self) -> Ordering {
T::interned_compare_key_ref(self.inner.borrow())
.cmp(&T::interned_compare_key_ref(other.inner.borrow()))
}
}
impl<T: ?Sized + 'static + Send + Sync + InternedCompare, C: InternContext> Hash for Guard<T, C> {
fn hash<H: Hasher>(&self, state: &mut H) {
T::interned_compare_key_ref(self.inner.borrow()).hash(state);
}
}
impl<T: Clone + Send + Sync + 'static + Hash + Eq, C: InternContext> Intern<C> for T {
fn intern_with_ctx(&self, context: &C) -> Interned<Self, C> {
Self::intern_cow_with_ctx(Cow::Borrowed(self), context)
impl<T: Clone + Send + Sync + 'static + Hash + Eq> Intern for T {
fn intern(&self) -> Interned<Self> {
Self::intern_cow(Cow::Borrowed(self))
}
fn intern_owned_with_ctx(this: <Self as ToOwned>::Owned, context: &C) -> Interned<Self, C>
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self>
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<Self, C>
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self>
where
Self: ToOwned,
{
context.interner().intern_sized(context, this)
Interner::get().intern_sized(this)
}
}
impl<T: Clone + Send + Sync + 'static + Hash + Eq, C: InternContext> Intern<C> for [T] {
fn intern_with_ctx(&self, context: &C) -> Interned<Self, C> {
Self::intern_cow_with_ctx(Cow::Borrowed(self), context)
impl<T: Clone + Send + Sync + 'static + Hash + Eq> Intern for [T] {
fn intern(&self) -> Interned<Self> {
Self::intern_cow(Cow::Borrowed(self))
}
fn intern_owned_with_ctx(this: <Self as ToOwned>::Owned, context: &C) -> Interned<Self, C>
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self>
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<Self, C>
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self>
where
Self: ToOwned,
{
context.interner().intern_slice(context, this)
Interner::get().intern_slice(this)
}
}
impl<C: BitSliceInternContext> Intern<C> for BitSlice {
fn intern_with_ctx(&self, context: &C) -> Interned<Self, C> {
Self::intern_cow_with_ctx(Cow::Borrowed(self), context)
impl Intern for BitSlice {
fn intern(&self) -> Interned<Self> {
Self::intern_cow(Cow::Borrowed(self))
}
fn intern_owned_with_ctx(this: <Self as ToOwned>::Owned, context: &C) -> Interned<Self, C>
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self>
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<Self, C>
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self>
where
Self: ToOwned,
{
context.interner().intern_bit_slice(context, this)
Interner::get().intern_bit_slice(this)
}
}
impl<C: InternContext> Intern<C> for str {
fn intern_with_ctx(&self, context: &C) -> Interned<Self, C> {
Self::intern_cow_with_ctx(Cow::Borrowed(self), context)
impl Intern for str {
fn intern(&self) -> Interned<Self> {
Self::intern_cow(Cow::Borrowed(self))
}
fn intern_owned_with_ctx(this: <Self as ToOwned>::Owned, context: &C) -> Interned<Self, C>
fn intern_owned(this: <Self as ToOwned>::Owned) -> Interned<Self>
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<Self, C>
fn intern_cow(this: Cow<'_, Self>) -> Interned<Self>
where
Self: ToOwned,
{
context.interner().intern_str(context, this)
Interner::get().intern_str(this)
}
}

View file

@ -330,6 +330,6 @@ impl<T: Type> Index<T> 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()))
}
}