48 lines
931 B
Rust
48 lines
931 B
Rust
|
|
use crate::obj::prelude::*;
|
||
|
|
|
||
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||
|
|
pub struct ConstHandle(usize);
|
||
|
|
|
||
|
|
impl ConstHandle {
|
||
|
|
pub fn index(&self) -> usize {
|
||
|
|
self.0
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn new(handle: usize) -> Self {
|
||
|
|
ConstHandle(handle)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Debug, Default)]
|
||
|
|
pub struct ConstPool {
|
||
|
|
pool: Vec<ObjRef>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl ConstPool {
|
||
|
|
pub fn new() -> Self {
|
||
|
|
Default::default()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn push(&mut self, value: ObjRef) -> ConstHandle {
|
||
|
|
let hdl = ConstHandle::new(self.pool.len());
|
||
|
|
self.pool.push(value);
|
||
|
|
hdl
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn get(&self, hdl: ConstHandle) -> &ObjRef {
|
||
|
|
self.pool.get(hdl.index()).unwrap()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn len(&self) -> usize {
|
||
|
|
self.pool.len()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl std::ops::Index<ConstHandle> for ConstPool {
|
||
|
|
type Output = ObjRef;
|
||
|
|
|
||
|
|
fn index(&self, hdl: ConstHandle) -> &Self::Output {
|
||
|
|
self.get(hdl)
|
||
|
|
}
|
||
|
|
}
|