cranpose_core/anchor_id.rs
1use crate::slot;
2
3/// Stable identifier for a slot in the slot table.
4///
5/// Anchors provide positional stability: they maintain their identity even when
6/// the slot table is reorganized (e.g., during conditional rendering or group moves).
7/// This prevents effect states from being prematurely removed during recomposition.
8#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default)]
9pub struct AnchorId {
10 pub(crate) id: u32,
11 pub(crate) generation: u32,
12}
13
14impl AnchorId {
15 pub(crate) const INVALID: Self = Self {
16 id: 0,
17 generation: 0,
18 };
19
20 pub(crate) fn new(id: usize) -> Self {
21 Self {
22 id: slot::checked_usize_to_u32(id, "anchor id"),
23 generation: 1,
24 }
25 }
26
27 /// Check if this anchor is valid (non-zero).
28 pub fn is_valid(&self) -> bool {
29 self.id != 0
30 }
31}