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-07-09 15:54:07 +00:00
|
|
|
sync::Arc,
|
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-07-09 15:54:07 +00:00
|
|
|
pub struct SharedArena {
|
|
|
|
pub components: Arc<UnsafeCell<ScopeMap>>,
|
2021-05-16 06:55:16 +00:00
|
|
|
}
|
2021-07-09 15:54:07 +00:00
|
|
|
pub type ScopeMap = SlotMap<DefaultKey, Scope>;
|
2021-05-16 06:55:16 +00:00
|
|
|
|
|
|
|
enum MutStatus {
|
|
|
|
Immut,
|
|
|
|
Mut,
|
|
|
|
}
|
|
|
|
|
2021-07-09 15:54:07 +00:00
|
|
|
impl SharedArena {
|
2021-06-30 02:44:21 +00:00
|
|
|
pub fn new(arena: ScopeMap) -> Self {
|
2021-07-09 15:54:07 +00:00
|
|
|
let components = Arc::new(UnsafeCell::new(arena));
|
|
|
|
SharedArena { components }
|
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-07-09 15:54:07 +00:00
|
|
|
let inner = unsafe { &*self.components.get() };
|
2021-05-18 14:36:17 +00:00
|
|
|
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-07-09 15:54:07 +00:00
|
|
|
let inner = unsafe { &mut *self.components.get() };
|
2021-05-18 14:36:17 +00:00
|
|
|
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-07-09 15:54:07 +00:00
|
|
|
let inner = unsafe { &mut *self.components.get() };
|
2021-05-18 14:36:17 +00:00
|
|
|
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-07-09 15:54:07 +00:00
|
|
|
let inner = unsafe { &mut *self.components.get() };
|
2021-06-06 03:47:54 +00:00
|
|
|
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!()
|
|
|
|
}
|
|
|
|
}
|