dioxus/packages/core/src/arena.rs

162 lines
4.7 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-05-18 05:16:43 +00:00
};
2021-05-16 06:55:16 +00:00
use generational_arena::Arena;
use crate::innerlude::*;
2021-05-18 05:16:43 +00:00
#[derive(Clone)]
pub struct ScopeArena(pub Rc<RefCell<ScopeArenaInner>>);
2021-05-18 05:16:43 +00:00
pub struct ScopeArenaInner {
2021-05-16 06:55:16 +00:00
pub(crate) arena: UnsafeCell<Arena<Scope>>,
locks: HashMap<ScopeIdx, MutStatus>,
}
enum MutStatus {
Immut,
Mut,
}
// impl ScopeArenaInner {
// pub fn new(arena: Arena<Scope>) -> Self {
// ScopeArenaInner {
// arena: UnsafeCell::new(arena),
// locks: Default::default(),
// }
// }
// /// 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> {
// let inner = unsafe { &*self.arena.get() };
// let scope = inner.get(idx);
// scope.ok_or_else(|| Error::FatalInternal("Scope not found"))
// }
// /// 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> {
// let inner = unsafe { &mut *self.arena.get() };
// let scope = inner.get_mut(idx);
// scope.ok_or_else(|| Error::FatalInternal("Scope not found"))
// }
// fn inner(&self) -> &Arena<Scope> {
// todo!()
// }
// fn inner_mut(&mut self) -> &mut Arena<Scope> {
// todo!()
// }
// /// THIS METHOD IS CURRENTLY UNSAFE
// /// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
// pub fn with<T>(&self, f: impl FnOnce(&mut Arena<Scope>) -> T) -> Result<T> {
// let inner = unsafe { &mut *self.arena.get() };
// Ok(f(inner))
// // todo!()
// }
// 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!()
// }
// pub fn try_remove(&mut self, id: ScopeIdx) -> Result<Scope> {
// let inner = unsafe { &mut *self.arena.get() };
// inner
// .remove(id)
// .ok_or_else(|| Error::FatalInternal("Scope not found"))
// }
// unsafe fn inner_unchecked<'s>() -> &'s mut Arena<Scope> {
// todo!()
// }
// }
2021-05-16 06:55:16 +00:00
impl ScopeArena {
pub fn new(arena: Arena<Scope>) -> 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
}
/// 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> {
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
}
/// 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> {
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
}
fn inner(&self) -> &Arena<Scope> {
todo!()
}
fn inner_mut(&mut self) -> &mut Arena<Scope> {
todo!()
}
/// THIS METHOD IS CURRENTLY UNSAFE
/// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
pub fn with<T>(&self, f: impl FnOnce(&mut Arena<Scope>) -> T) -> Result<T> {
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!()
}
pub fn try_remove(&self, id: ScopeIdx) -> Result<Scope> {
let inner = unsafe { &mut *self.0.borrow().arena.get() };
inner
.remove(id)
.ok_or_else(|| Error::FatalInternal("Scope not found"))
}
2021-05-16 06:55:16 +00:00
unsafe fn inner_unchecked<'s>() -> &'s mut Arena<Scope> {
todo!()
}
}