dioxus/packages/core/src/arena.rs

82 lines
2.1 KiB
Rust
Raw Normal View History

use std::{cell::UnsafeCell, rc::Rc};
2021-05-16 06:55:16 +00:00
use crate::innerlude::*;
2021-07-20 23:03:49 +00:00
use slotmap::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: Rc<UnsafeCell<ScopeMap>>,
2021-05-16 06:55:16 +00:00
}
2021-07-20 23:03:49 +00:00
pub type ScopeMap = SlotMap<ScopeId, 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 {
let components = Rc::new(UnsafeCell::new(arena));
2021-07-09 15:54:07 +00:00
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
2021-07-18 07:54:42 +00:00
pub fn get(&self, idx: ScopeId) -> Option<&Scope> {
2021-07-09 15:54:07 +00:00
let inner = unsafe { &*self.components.get() };
2021-07-18 07:54:42 +00:00
inner.get(idx)
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
2021-07-18 07:54:42 +00:00
pub fn get_mut(&self, idx: ScopeId) -> Option<&mut Scope> {
2021-07-09 15:54:07 +00:00
let inner = unsafe { &mut *self.components.get() };
2021-07-18 07:54:42 +00:00
inner.get_mut(idx)
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,
_id: ScopeId,
2021-07-13 20:48:47 +00:00
_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,
_id: ScopeId,
2021-07-13 20:48:47 +00:00
_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: ScopeId) -> 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!()
}
}