From 8c95a2ba3d04ca8aee8fd1009679748ff528a4b8 Mon Sep 17 00:00:00 2001 From: Janis Date: Thu, 6 Mar 2025 02:18:15 +0100 Subject: [PATCH] single-threaded interior mutably wrapper for internpool --- src/ast2/intern.rs | 81 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 74 insertions(+), 7 deletions(-) diff --git a/src/ast2/intern.rs b/src/ast2/intern.rs index a02c3d2..6c3c2d5 100644 --- a/src/ast2/intern.rs +++ b/src/ast2/intern.rs @@ -402,6 +402,73 @@ impl Index { } } +pub struct InternPoolWrapper(core::cell::UnsafeCell); + +impl InternPoolWrapper { + pub fn as_mut(&self) -> &mut InternPool { + unsafe { &mut *self.0.get() } + } + pub fn as_ref(&self) -> &InternPool { + unsafe { &*self.0.get() } + } + + pub fn new() -> Self { + InternPool::new().into() + } +} + +impl From for InternPoolWrapper { + fn from(value: InternPool) -> Self { + Self(core::cell::UnsafeCell::new(value)) + } +} + +impl core::ops::Deref for InternPoolWrapper { + type Target = InternPool; + + fn deref(&self) -> &Self::Target { + unsafe { &*self.0.get() } + } +} + +impl core::ops::DerefMut for InternPoolWrapper { + fn deref_mut(&mut self) -> &mut Self::Target { + self.as_mut() + } +} + +impl AsRef for InternPoolWrapper { + fn as_ref(&self) -> &InternPool { + Self::as_ref(self) + } +} + +impl AsRef for InternPool { + fn as_ref(&self) -> &InternPool { + self + } +} + +// impl AsMut for InternPoolWrapper { +// fn as_mut(&mut self) -> &mut InternPool { +// Self::as_mut(self) +// } +// } + +// impl AsMut for InternPool { +// fn as_mut(&mut self) -> &mut InternPool { +// self +// } +// } + +impl core::fmt::Debug for InternPoolWrapper { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_tuple("InternPoolWrapper") + .field(self.as_ref()) + .finish() + } +} + pub struct InternPool { tags: Vec, indices: Vec, @@ -1308,6 +1375,13 @@ impl InternPool { }) } + pub fn try_get_pointee_type(&self, pointer: Index) -> Option { + match self.get_key(pointer) { + Key::PointerType { pointee, .. } | Key::ArrayType { pointee, .. } => Some(pointee), + _ => None, + } + } + pub fn get_pointer_type(&mut self, pointee: Index, flags: Option) -> Index { let key = Key::PointerType { pointee, @@ -1316,13 +1390,6 @@ impl InternPool { self.get_or_insert(key) } - pub fn try_get_pointee_type(&self, pointer: Index) -> Option { - match self.get_key(pointer) { - Key::PointerType { pointee, .. } | Key::ArrayType { pointee, .. } => Some(pointee), - _ => None, - } - } - pub fn try_get_pointer_type( &self, pointee: Index,