dioxus/packages/core/src/bump_frame.rs

39 lines
949 B
Rust
Raw Normal View History

2022-12-03 00:24:49 +00:00
use crate::nodes::RenderReturn;
2022-11-20 01:07:29 +00:00
use bumpalo::Bump;
2023-01-09 21:50:33 +00:00
use std::cell::{Cell, UnsafeCell};
2022-11-29 21:31:04 +00:00
pub(crate) struct BumpFrame {
2023-01-09 21:50:33 +00:00
pub bump: UnsafeCell<Bump>,
2022-11-29 21:31:04 +00:00
pub node: Cell<*const RenderReturn<'static>>,
}
2022-11-20 01:07:29 +00:00
impl BumpFrame {
2022-12-03 00:24:49 +00:00
pub(crate) fn new(capacity: usize) -> Self {
let bump = Bump::with_capacity(capacity);
Self {
2023-01-09 21:50:33 +00:00
bump: UnsafeCell::new(bump),
2022-11-29 21:31:04 +00:00
node: Cell::new(std::ptr::null()),
}
}
2022-11-20 01:07:29 +00:00
/// Creates a new lifetime out of thin air
2022-12-03 00:24:49 +00:00
pub(crate) unsafe fn try_load_node<'b>(&self) -> Option<&'b RenderReturn<'b>> {
2022-11-29 21:31:04 +00:00
let node = self.node.get();
if node.is_null() {
return None;
}
unsafe { std::mem::transmute(&*node) }
2022-11-12 02:29:27 +00:00
}
2023-01-09 21:50:33 +00:00
pub(crate) fn bump(&self) -> &Bump {
unsafe { &*self.bump.get() }
}
2023-01-11 00:39:56 +00:00
#[allow(clippy::mut_from_ref)]
2023-01-09 21:50:33 +00:00
pub(crate) unsafe fn bump_mut(&self) -> &mut Bump {
unsafe { &mut *self.bump.get() }
}
}