mlua-luau-scheduler/lib/result_map.rs
2024-01-31 20:24:14 +01:00

46 lines
1.2 KiB
Rust

#![allow(clippy::inline_always)]
use std::{
cell::RefCell,
collections::{HashMap, HashSet},
rc::Rc,
};
use crate::{thread_id::ThreadId, util::ThreadResult};
#[derive(Clone)]
pub(crate) struct ThreadResultMap {
tracked: Rc<RefCell<HashSet<ThreadId>>>,
inner: Rc<RefCell<HashMap<ThreadId, ThreadResult>>>,
}
impl ThreadResultMap {
pub fn new() -> Self {
Self {
tracked: Rc::new(RefCell::new(HashSet::new())),
inner: Rc::new(RefCell::new(HashMap::new())),
}
}
#[inline(always)]
pub fn track(&self, id: ThreadId) {
self.tracked.borrow_mut().insert(id);
}
#[inline(always)]
pub fn is_tracked(&self, id: ThreadId) -> bool {
self.tracked.borrow().contains(&id)
}
#[inline(always)]
pub fn insert(&self, id: ThreadId, result: ThreadResult) {
debug_assert!(self.is_tracked(id), "Thread must be tracked");
self.inner.borrow_mut().insert(id, result);
}
pub fn remove(&self, id: ThreadId) -> Option<ThreadResult> {
let res = self.inner.borrow_mut().remove(&id)?;
self.tracked.borrow_mut().remove(&id);
Some(res)
}
}