diff --git a/compiler/cl-structures/src/deprecated_intern_pool.rs b/compiler/cl-structures/src/index_map.rs similarity index 65% rename from compiler/cl-structures/src/deprecated_intern_pool.rs rename to compiler/cl-structures/src/index_map.rs index 1f7a417..f1fdffa 100644 --- a/compiler/cl-structures/src/deprecated_intern_pool.rs +++ b/compiler/cl-structures/src/index_map.rs @@ -1,10 +1,10 @@ -//! Trivially-copyable, easily comparable typed indices, and a [Pool] to contain them +//! Trivially-copyable, easily comparable typed [indices](MapIndex), and a [Pool] to contain them //! //! # Examples //! //! ```rust -//! # use cl_structures::deprecated_intern_pool::*; -//! // first, create a new InternKey type (this ensures type safety) +//! # use cl_structures::index_map::*; +//! // first, create a new MapIndex type (this ensures type safety) //! make_intern_key!{ //! NumbersKey //! } @@ -29,18 +29,16 @@ /// Creates newtype indices over [`usize`] for use as [Pool] keys. #[macro_export] -macro_rules! make_intern_key {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$( +macro_rules! make_index {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$( $(#[$meta])* #[repr(transparent)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct $name(usize); - impl $crate::deprecated_intern_pool::InternKey for $name { + impl $crate::index_map::MapIndex for $name { #[doc = concat!("Constructs a [`", stringify!($name), "`] from a [`usize`] without checking bounds.\n")] - /// # Safety - /// /// The provided value should be within the bounds of its associated container - fn from_raw_unchecked(value: usize) -> Self { + fn from_usize(value: usize) -> Self { Self(value) } fn get(&self) -> usize { @@ -56,93 +54,93 @@ macro_rules! make_intern_key {($($(#[$meta:meta])* $name:ident),*$(,)?) => {$( use core::slice::GetManyMutError; use std::ops::{Index, IndexMut}; -pub use make_intern_key; +pub use make_index; -use self::iter::InternKeyIter; +use self::iter::MapIndexIter; /// An index into a [Pool]. For full type-safety, /// there should be a unique [InternKey] for each [Pool] -pub trait InternKey: std::fmt::Debug { - /// Constructs an [`InternKey`] from a [`usize`] without checking bounds. +pub trait MapIndex: std::fmt::Debug { + /// Constructs an [`MapIndex`] from a [`usize`] without checking bounds. /// /// # Safety /// /// The provided value should be within the bounds of its associated container. // ID::from_raw_unchecked here isn't *actually* unsafe, since bounds should always be // checked, however, the function has unverifiable preconditions. - fn from_raw_unchecked(value: usize) -> Self; + fn from_usize(value: usize) -> Self; /// Gets the index of the [`InternKey`] by value fn get(&self) -> usize; } #[derive(Clone, Debug, PartialEq, Eq)] -pub struct Pool { - pool: Vec, - id_type: std::marker::PhantomData, +pub struct IndexMap { + pool: Vec, + id_type: std::marker::PhantomData, } -impl Pool { +impl IndexMap { pub fn new() -> Self { Self::default() } - pub fn get(&self, index: ID) -> Option<&T> { + pub fn get(&self, index: K) -> Option<&V> { self.pool.get(index.get()) } - pub fn get_mut(&mut self, index: ID) -> Option<&mut T> { + pub fn get_mut(&mut self, index: K) -> Option<&mut V> { self.pool.get_mut(index.get()) } pub fn get_many_mut( &mut self, - indices: [ID; N], - ) -> Result<[&mut T; N], GetManyMutError> { + indices: [K; N], + ) -> Result<[&mut V; N], GetManyMutError> { self.pool.get_many_mut(indices.map(|id| id.get())) } - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator { self.pool.iter() } - pub fn iter_mut(&mut self) -> impl Iterator { + pub fn iter_mut(&mut self) -> impl Iterator { self.pool.iter_mut() } - pub fn key_iter(&self) -> iter::InternKeyIter { + pub fn key_iter(&self) -> iter::MapIndexIter { // Safety: Pool currently has pool.len() entries, and data cannot be removed - InternKeyIter::new(0..self.pool.len()) + MapIndexIter::new(0..self.pool.len()) } /// Constructs an [ID](InternKey) from a [usize], if it's within bounds #[doc(hidden)] - pub fn try_key_from(&self, value: usize) -> Option { - (value < self.pool.len()).then(|| ID::from_raw_unchecked(value)) + pub fn try_key_from(&self, value: usize) -> Option { + (value < self.pool.len()).then(|| K::from_usize(value)) } - pub fn insert(&mut self, value: T) -> ID { + pub fn insert(&mut self, value: V) -> K { let id = self.pool.len(); self.pool.push(value); // Safety: value was pushed to `self.pool[id]` - ID::from_raw_unchecked(id) + K::from_usize(id) } } -impl Default for Pool { +impl Default for IndexMap { fn default() -> Self { Self { pool: vec![], id_type: std::marker::PhantomData } } } -impl Index for Pool { - type Output = T; +impl Index for IndexMap { + type Output = V; - fn index(&self, index: ID) -> &Self::Output { + fn index(&self, index: K) -> &Self::Output { match self.pool.get(index.get()) { None => panic!("Index {:?} out of bounds in pool!", index), Some(value) => value, } } } -impl IndexMut for Pool { - fn index_mut(&mut self, index: ID) -> &mut Self::Output { +impl IndexMut for IndexMap { + fn index_mut(&mut self, index: K) -> &mut Self::Output { match self.pool.get_mut(index.get()) { None => panic!("Index {:?} out of bounds in pool!", index), Some(value) => value, @@ -153,7 +151,7 @@ impl IndexMut for Pool { mod iter { use std::{marker::PhantomData, ops::Range}; - use super::InternKey; + use super::MapIndex; /// Iterates over the keys of a [Pool](super::Pool) independently of the pool itself. /// @@ -161,12 +159,12 @@ mod iter { /// but is *NOT* guaranteed to iterate over all elements of the pool /// if the pool is extended during iteration. #[derive(Clone, Debug, PartialEq, Eq, Hash)] - pub struct InternKeyIter { + pub struct MapIndexIter { range: Range, - _id: PhantomData, + _id: PhantomData, } - impl InternKeyIter { + impl MapIndexIter { /// Creates a new [InternKeyIter] producing the given [InternKey] /// /// # Safety: @@ -178,22 +176,22 @@ mod iter { } } - impl Iterator for InternKeyIter { + impl Iterator for MapIndexIter { type Item = ID; fn next(&mut self) -> Option { // Safety: InternKeyIter can only be created by InternKeyIter::new() - Some(ID::from_raw_unchecked(self.range.next()?)) + Some(ID::from_usize(self.range.next()?)) } fn size_hint(&self) -> (usize, Option) { self.range.size_hint() } } - impl DoubleEndedIterator for InternKeyIter { + impl DoubleEndedIterator for MapIndexIter { fn next_back(&mut self) -> Option { // Safety: see above - Some(ID::from_raw_unchecked(self.range.next_back()?)) + Some(ID::from_usize(self.range.next_back()?)) } } - impl ExactSizeIterator for InternKeyIter {} + impl ExactSizeIterator for MapIndexIter {} } diff --git a/compiler/cl-structures/src/lib.rs b/compiler/cl-structures/src/lib.rs index ea4d35e..df8b6e5 100644 --- a/compiler/cl-structures/src/lib.rs +++ b/compiler/cl-structures/src/lib.rs @@ -18,4 +18,4 @@ pub mod tree; pub mod stack; -pub mod deprecated_intern_pool; +pub mod index_map; diff --git a/compiler/cl-typeck/src/key.rs b/compiler/cl-typeck/src/key.rs index eb28a29..7cebd05 100644 --- a/compiler/cl-typeck/src/key.rs +++ b/compiler/cl-typeck/src/key.rs @@ -1,7 +1,7 @@ -use cl_structures::deprecated_intern_pool::*; +use cl_structures::index_map::*; // define the index types -make_intern_key! { +make_index! { /// Uniquely represents a [Def][1] in the [Def][1] [Pool] /// /// [1]: crate::definition::Def diff --git a/compiler/cl-typeck/src/module.rs b/compiler/cl-typeck/src/module.rs index 2ce88ab..ebb6018 100644 --- a/compiler/cl-typeck/src/module.rs +++ b/compiler/cl-typeck/src/module.rs @@ -1,7 +1,7 @@ //! A [Module] is a node in the Module Tree (a component of a //! [Project](crate::project::Project)) use cl_ast::Sym; -use cl_structures::deprecated_intern_pool::InternKey; +use cl_structures::index_map::MapIndex; use crate::key::DefID; use std::collections::HashMap; diff --git a/compiler/cl-typeck/src/project.rs b/compiler/cl-typeck/src/project.rs index 3f4b7ef..495e7ac 100644 --- a/compiler/cl-typeck/src/project.rs +++ b/compiler/cl-typeck/src/project.rs @@ -7,7 +7,7 @@ use crate::{ path::Path, }; use cl_ast::PathPart; -use cl_structures::deprecated_intern_pool::Pool; +use cl_structures::index_map::IndexMap; use std::{ collections::HashMap, ops::{Index, IndexMut}, @@ -17,7 +17,7 @@ use self::evaluate::EvaluableTypeExpression; #[derive(Clone, Debug)] pub struct Project<'a> { - pub pool: Pool, DefID>, + pub pool: IndexMap>, /// Stores anonymous tuples, function pointer types, etc. pub anon_types: HashMap, pub root: DefID, @@ -33,7 +33,7 @@ impl Default for Project<'_> { fn default() -> Self { const ROOT_PATH: cl_ast::Path = cl_ast::Path { absolute: true, parts: Vec::new() }; - let mut pool = Pool::default(); + let mut pool = IndexMap::default(); let root = pool.insert(Def { module: Default::default(), kind: DefKind::Type(TypeKind::Module),