dioxus/packages/core/src/arena.rs

38 lines
907 B
Rust
Raw Normal View History

2022-11-09 03:39:37 +00:00
use crate::{nodes::VNode, virtual_dom::VirtualDom};
2022-11-02 01:42:29 +00:00
2022-11-16 00:05:22 +00:00
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ElementId(pub usize);
2022-11-02 01:42:29 +00:00
pub struct ElementPath {
pub template: *mut VNode<'static>,
pub element: usize,
}
2022-11-12 02:29:27 +00:00
impl ElementPath {
pub fn null() -> Self {
Self {
template: std::ptr::null_mut(),
element: 0,
}
}
}
2022-11-02 01:42:29 +00:00
impl VirtualDom {
pub fn next_element(&mut self, template: &VNode) -> ElementId {
let entry = self.elements.vacant_entry();
let id = entry.key();
2022-11-02 01:42:29 +00:00
entry.insert(ElementPath {
template: template as *const _ as *mut _,
element: id,
});
2022-11-02 01:42:29 +00:00
ElementId(id)
}
2022-11-02 01:42:29 +00:00
pub fn cleanup_element(&mut self, id: ElementId) {
self.elements.remove(id.0);
}
}