whisker_runtime/view/handle.rs
1//! [`Element`] — opaque, `Copy`, backend-agnostic identifier.
2//!
3//! IDs are allocated by the renderer's [`create_element`] call and
4//! are valid until [`release_element`] (or the renderer being
5//! uninstalled). They have no semantic meaning to user code beyond
6//! "name this element in subsequent renderer calls"; the renderer
7//! is free to use them as indices, hash keys, or whatever fits.
8//!
9//! [`create_element`]: super::create_element
10//! [`release_element`]: super::release_element
11
12/// Backend-agnostic element handle. `Copy` so it threads through
13/// reactive closures without lifetime gymnastics.
14#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
15pub struct Element(pub(crate) u32);
16
17impl Element {
18 /// The numeric id this handle wraps. Mostly useful for renderers
19 /// that store per-element state in side maps.
20 pub fn id(self) -> u32 {
21 self.0
22 }
23
24 /// Construct a handle from a raw id. Use this only when bridging
25 /// from a renderer-internal map (e.g. the `MockRenderer` test
26 /// fixture); otherwise let [`create_element`] hand them out.
27 ///
28 /// [`create_element`]: super::create_element
29 pub fn from_raw(id: u32) -> Self {
30 Self(id)
31 }
32}