arena.rs 2.5 KB

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