1
0

arena.rs 2.5 KB

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