2021-05-18 05:16:43 +00:00
|
|
|
use std::{
|
|
|
|
cell::{RefCell, UnsafeCell},
|
|
|
|
collections::HashMap,
|
2021-06-03 17:57:41 +00:00
|
|
|
rc::Rc,
|
2021-05-18 05:16:43 +00:00
|
|
|
};
|
2021-05-16 06:55:16 +00:00
|
|
|
|
|
|
|
use crate::innerlude::*;
|
2021-06-30 02:44:21 +00:00
|
|
|
use slotmap::{DefaultKey, SlotMap};
|
2021-05-16 06:55:16 +00:00
|
|
|
|
2021-05-18 05:16:43 +00:00
|
|
|
#[derive(Clone)]
|
2021-06-06 03:47:54 +00:00
|
|
|
pub struct ScopeArena(pub Rc<RefCell<ScopeArenaInner>>);
|
2021-06-30 02:44:21 +00:00
|
|
|
pub type ScopeMap = SlotMap<DefaultKey, Scope>;
|
2021-05-18 05:16:43 +00:00
|
|
|
|
2021-06-06 03:47:54 +00:00
|
|
|
pub struct ScopeArenaInner {
|
2021-06-30 02:44:21 +00:00
|
|
|
pub(crate) arena: UnsafeCell<ScopeMap>,
|
2021-05-16 06:55:16 +00:00
|
|
|
locks: HashMap<ScopeIdx, MutStatus>,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum MutStatus {
|
|
|
|
Immut,
|
|
|
|
Mut,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScopeArena {
|
2021-06-30 02:44:21 +00:00
|
|
|
pub fn new(arena: ScopeMap) -> Self {
|
2021-05-18 05:16:43 +00:00
|
|
|
ScopeArena(Rc::new(RefCell::new(ScopeArenaInner {
|
2021-05-16 06:55:16 +00:00
|
|
|
arena: UnsafeCell::new(arena),
|
|
|
|
locks: Default::default(),
|
2021-05-18 05:16:43 +00:00
|
|
|
})))
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:36:17 +00:00
|
|
|
/// THIS METHOD IS CURRENTLY UNSAFE
|
|
|
|
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
|
2021-05-16 06:58:57 +00:00
|
|
|
pub fn try_get(&self, idx: ScopeIdx) -> Result<&Scope> {
|
2021-05-18 14:36:17 +00:00
|
|
|
let inner = unsafe { &*self.0.borrow().arena.get() };
|
|
|
|
let scope = inner.get(idx);
|
|
|
|
scope.ok_or_else(|| Error::FatalInternal("Scope not found"))
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 14:36:17 +00:00
|
|
|
/// THIS METHOD IS CURRENTLY UNSAFE
|
|
|
|
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
|
2021-05-16 06:58:57 +00:00
|
|
|
pub fn try_get_mut(&self, idx: ScopeIdx) -> Result<&mut Scope> {
|
2021-05-18 14:36:17 +00:00
|
|
|
let inner = unsafe { &mut *self.0.borrow().arena.get() };
|
|
|
|
let scope = inner.get_mut(idx);
|
|
|
|
scope.ok_or_else(|| Error::FatalInternal("Scope not found"))
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 02:44:21 +00:00
|
|
|
fn inner(&self) -> &ScopeMap {
|
2021-05-16 06:55:16 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-06-30 02:44:21 +00:00
|
|
|
fn inner_mut(&mut self) -> &mut ScopeMap {
|
2021-05-16 06:55:16 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-05-18 14:36:17 +00:00
|
|
|
/// THIS METHOD IS CURRENTLY UNSAFE
|
|
|
|
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
|
2021-06-30 02:44:21 +00:00
|
|
|
pub fn with<T>(&self, f: impl FnOnce(&mut ScopeMap) -> T) -> Result<T> {
|
2021-05-18 14:36:17 +00:00
|
|
|
let inner = unsafe { &mut *self.0.borrow().arena.get() };
|
|
|
|
Ok(f(inner))
|
|
|
|
// todo!()
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 18:14:49 +00:00
|
|
|
pub fn with_scope<'b, O: 'static>(
|
|
|
|
&'b self,
|
|
|
|
id: ScopeIdx,
|
|
|
|
f: impl FnOnce(&'b mut Scope) -> O,
|
|
|
|
) -> Result<O> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
// return a bumpframe with a lifetime attached to the arena borrow
|
|
|
|
// this is useful for merging lifetimes
|
|
|
|
pub fn with_scope_vnode<'b>(
|
|
|
|
&self,
|
|
|
|
id: ScopeIdx,
|
|
|
|
f: impl FnOnce(&mut Scope) -> &VNode<'b>,
|
|
|
|
) -> Result<&VNode<'b>> {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:05:17 +00:00
|
|
|
pub fn try_remove(&self, id: ScopeIdx) -> Result<Scope> {
|
2021-06-06 03:47:54 +00:00
|
|
|
let inner = unsafe { &mut *self.0.borrow().arena.get() };
|
|
|
|
inner
|
|
|
|
.remove(id)
|
|
|
|
.ok_or_else(|| Error::FatalInternal("Scope not found"))
|
|
|
|
}
|
|
|
|
|
2021-06-30 02:44:21 +00:00
|
|
|
unsafe fn inner_unchecked<'s>() -> &'s mut ScopeMap {
|
2021-05-16 06:55:16 +00:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|