Make Caches a struct.

This commit is contained in:
Stephen Chung 2022-05-24 19:34:47 +08:00
parent 52bb6e68e8
commit 0e363bd6b7

View File

@ -29,40 +29,48 @@ pub type FnResolutionCache = BTreeMap<u64, Option<FnResolutionCacheEntry>>;
/// The following caches are contained inside this type:
/// * A stack of [function resolution caches][FnResolutionCache]
#[derive(Debug, Clone)]
pub struct Caches<'a>(StaticVec<FnResolutionCache>, PhantomData<&'a ()>);
pub struct Caches<'a> {
/// Stack of [function resolution caches][FnResolutionCache].
fn_resolution: StaticVec<FnResolutionCache>,
/// Take care of the lifetime parameter.
dummy: PhantomData<&'a ()>,
}
impl Caches<'_> {
/// Create an empty [`Caches`].
#[inline(always)]
#[must_use]
pub const fn new() -> Self {
Self(StaticVec::new_const(), PhantomData)
Self {
fn_resolution: StaticVec::new_const(),
dummy: PhantomData,
}
}
/// Get the number of function resolution cache(s) in the stack.
#[inline(always)]
#[must_use]
pub fn fn_resolution_caches_len(&self) -> usize {
self.0.len()
self.fn_resolution.len()
}
/// Get a mutable reference to the current function resolution cache.
#[inline]
#[must_use]
pub fn fn_resolution_cache_mut(&mut self) -> &mut FnResolutionCache {
if self.0.is_empty() {
if self.fn_resolution.is_empty() {
// Push a new function resolution cache if the stack is empty
self.push_fn_resolution_cache();
}
self.0.last_mut().unwrap()
self.fn_resolution.last_mut().unwrap()
}
/// Push an empty function resolution cache onto the stack and make it current.
#[allow(dead_code)]
#[inline(always)]
pub fn push_fn_resolution_cache(&mut self) {
self.0.push(BTreeMap::new());
self.fn_resolution.push(BTreeMap::new());
}
/// Rewind the function resolution caches stack to a particular size.
#[inline(always)]
pub fn rewind_fn_resolution_caches(&mut self, len: usize) {
self.0.truncate(len);
self.fn_resolution.truncate(len);
}
}