use crate::slot;
/// Stable identifier for a slot in the slot table.
///
/// Anchors provide positional stability: they maintain their identity even when
/// the slot table is reorganized (e.g., during conditional rendering or group moves).
/// This prevents effect states from being prematurely removed during recomposition.
#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, Default)]
pub struct AnchorId {
pub(crate) id: u32,
pub(crate) generation: u32,
}
impl AnchorId {
pub(crate) const INVALID: Self = Self {
id: 0,
generation: 0,
};
pub(crate) fn new(id: usize) -> Self {
Self {
id: slot::checked_usize_to_u32(id, "anchor id"),
generation: 1,
}
}
/// Check if this anchor is valid (non-zero).
pub fn is_valid(&self) -> bool {
self.id != 0
}
}