bump_frame.rs 949 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. use crate::nodes::RenderReturn;
  2. use bumpalo::Bump;
  3. use std::cell::{Cell, UnsafeCell};
  4. pub(crate) struct BumpFrame {
  5. pub bump: UnsafeCell<Bump>,
  6. pub node: Cell<*const RenderReturn<'static>>,
  7. }
  8. impl BumpFrame {
  9. pub(crate) fn new(capacity: usize) -> Self {
  10. let bump = Bump::with_capacity(capacity);
  11. Self {
  12. bump: UnsafeCell::new(bump),
  13. node: Cell::new(std::ptr::null()),
  14. }
  15. }
  16. /// Creates a new lifetime out of thin air
  17. pub(crate) unsafe fn try_load_node<'b>(&self) -> Option<&'b RenderReturn<'b>> {
  18. let node = self.node.get();
  19. if node.is_null() {
  20. return None;
  21. }
  22. unsafe { std::mem::transmute(&*node) }
  23. }
  24. pub(crate) fn bump(&self) -> &Bump {
  25. unsafe { &*self.bump.get() }
  26. }
  27. #[allow(clippy::mut_from_ref)]
  28. pub(crate) unsafe fn bump_mut(&self) -> &mut Bump {
  29. unsafe { &mut *self.bump.get() }
  30. }
  31. }