Push wrapped layout interners through

This commit is contained in:
Ayaz Hafiz 2023-01-03 10:51:33 -06:00
parent 947158b17e
commit b60d5c0251
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
7 changed files with 86 additions and 40 deletions

View file

@ -23,7 +23,7 @@ use std::hash::{Hash, Hasher};
use ven_pretty::{DocAllocator, DocBuilder};
mod intern;
pub use intern::{STLayoutInterner, TLLayoutInterner};
pub use intern::{GlobalLayoutInterner, LayoutInterner, STLayoutInterner, TLLayoutInterner};
// if your changes cause this number to go down, great!
// please change it to the lower number.

View file

@ -1,6 +1,60 @@
use roc_intern::{SingleThreadedInterner, ThreadLocalInterner};
use std::sync::Arc;
use roc_intern::{GlobalInterner, Interned, Interner, SingleThreadedInterner, ThreadLocalInterner};
use super::Layout;
pub type TLLayoutInterner<'a> = ThreadLocalInterner<'a, Layout<'a>>;
pub type STLayoutInterner<'a> = SingleThreadedInterner<'a, Layout<'a>>;
pub trait LayoutInterner<'a>: Interner<'a, Layout<'a>> {}
#[derive(Debug)]
pub struct GlobalLayoutInterner<'a>(Arc<GlobalInterner<'a, Layout<'a>>>);
#[derive(Debug)]
pub struct TLLayoutInterner<'a>(ThreadLocalInterner<'a, Layout<'a>>);
#[derive(Debug)]
pub struct STLayoutInterner<'a>(SingleThreadedInterner<'a, Layout<'a>>);
impl<'a> GlobalLayoutInterner<'a> {
pub fn with_capacity(capacity: usize) -> Self {
Self(GlobalInterner::with_capacity(capacity))
}
pub fn fork(&self) -> TLLayoutInterner<'a> {
TLLayoutInterner(self.0.fork())
}
pub fn unwrap(self) -> Result<STLayoutInterner<'a>, Self> {
match self.0.unwrap() {
Ok(st) => Ok(STLayoutInterner(st)),
Err(global) => Err(Self(global)),
}
}
}
impl<'a> STLayoutInterner<'a> {
pub fn with_capacity(capacity: usize) -> Self {
Self(SingleThreadedInterner::with_capacity(capacity))
}
pub fn into_global(self) -> GlobalLayoutInterner<'a> {
GlobalLayoutInterner(self.0.into_global())
}
}
impl<'a> Interner<'a, Layout<'a>> for TLLayoutInterner<'a> {
fn insert(&mut self, value: &'a Layout<'a>) -> Interned<Layout<'a>> {
self.0.insert(value)
}
fn get(&self, key: Interned<Layout<'a>>) -> &'a Layout<'a> {
self.0.get(key)
}
}
impl<'a> LayoutInterner<'a> for TLLayoutInterner<'a> {}
impl<'a> Interner<'a, Layout<'a>> for STLayoutInterner<'a> {
fn insert(&mut self, value: &'a Layout<'a>) -> Interned<Layout<'a>> {
self.0.insert(value)
}
fn get(&self, key: Interned<Layout<'a>>) -> &'a Layout<'a> {
self.0.get(key)
}
}
impl<'a> LayoutInterner<'a> for STLayoutInterner<'a> {}