arena.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. use std::{cell::UnsafeCell, rc::Rc};
  2. use crate::innerlude::*;
  3. use slotmap::SlotMap;
  4. #[derive(Clone)]
  5. pub struct SharedArena {
  6. pub components: Rc<UnsafeCell<ScopeMap>>,
  7. }
  8. pub type ScopeMap = SlotMap<ScopeId, Scope>;
  9. enum MutStatus {
  10. Immut,
  11. Mut,
  12. }
  13. impl SharedArena {
  14. pub fn new(arena: ScopeMap) -> Self {
  15. let components = Rc::new(UnsafeCell::new(arena));
  16. SharedArena { components }
  17. }
  18. /// THIS METHOD IS CURRENTLY UNSAFE
  19. /// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
  20. pub fn get(&self, idx: ScopeId) -> Option<&Scope> {
  21. let inner = unsafe { &*self.components.get() };
  22. inner.get(idx)
  23. }
  24. /// THIS METHOD IS CURRENTLY UNSAFE
  25. /// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
  26. pub fn get_mut(&self, idx: ScopeId) -> Option<&mut Scope> {
  27. let inner = unsafe { &mut *self.components.get() };
  28. inner.get_mut(idx)
  29. }
  30. fn inner(&self) -> &ScopeMap {
  31. todo!()
  32. }
  33. fn inner_mut(&mut self) -> &mut ScopeMap {
  34. todo!()
  35. }
  36. /// THIS METHOD IS CURRENTLY UNSAFE
  37. /// THERE ARE NO CHECKS TO VERIFY THAT WE ARE ALLOWED TO DO THIS
  38. pub fn with<T>(&self, f: impl FnOnce(&mut ScopeMap) -> T) -> Result<T> {
  39. let inner = unsafe { &mut *self.components.get() };
  40. Ok(f(inner))
  41. // todo!()
  42. }
  43. pub fn with_scope<'b, O: 'static>(
  44. &'b self,
  45. _id: ScopeId,
  46. _f: impl FnOnce(&'b mut Scope) -> O,
  47. ) -> Result<O> {
  48. todo!()
  49. }
  50. // return a bumpframe with a lifetime attached to the arena borrow
  51. // this is useful for merging lifetimes
  52. pub fn with_scope_vnode<'b>(
  53. &self,
  54. _id: ScopeId,
  55. _f: impl FnOnce(&mut Scope) -> &VNode<'b>,
  56. ) -> Result<&VNode<'b>> {
  57. todo!()
  58. }
  59. pub fn try_remove(&self, id: ScopeId) -> Result<Scope> {
  60. let inner = unsafe { &mut *self.components.get() };
  61. inner
  62. .remove(id)
  63. .ok_or_else(|| Error::FatalInternal("Scope not found"))
  64. }
  65. unsafe fn inner_unchecked<'s>() -> &'s mut ScopeMap {
  66. todo!()
  67. }
  68. }