dioxus/packages/core/src/arena.rs

89 lines
2.3 KiB
Rust
Raw Normal View History

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
}
/// THIS METHOD IS CURRENTLY UNSAFE
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
pub fn try_get(&self, idx: ScopeIdx) -> Result<&Scope> {
2021-07-09 15:54:07 +00:00
let inner = unsafe { &*self.components.get() };
let scope = inner.get(idx);
scope.ok_or_else(|| Error::FatalInternal("Scope not found"))
2021-05-16 06:55:16 +00:00
}
/// THIS METHOD IS CURRENTLY UNSAFE
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
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() };
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!()
}
/// 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() };
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,
2021-07-13 20:48:47 +00:00
_id: ScopeIdx,
_f: impl FnOnce(&'b mut Scope) -> O,
2021-06-07 18:14:49 +00:00
) -> 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,
2021-07-13 20:48:47 +00:00
_id: ScopeIdx,
_f: impl FnOnce(&mut Scope) -> &VNode<'b>,
2021-06-07 18:14:49 +00:00
) -> Result<&VNode<'b>> {
todo!()
}
pub fn try_remove(&self, id: ScopeIdx) -> Result<Scope> {
2021-07-09 15:54:07 +00:00
let inner = unsafe { &mut *self.components.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!()
}
}