#[repr(transparent)]pub struct GenIndex(pub u64);Expand description
Generation + index combined identifier used by the allocator.
The value stores a 16-bit generation in the high bits and a platform-sized
real index in the low bits. Concretely this crate places the generation in
the upper 16 bits and the real index in the lower 48 bits of the u64
storage. GenIndex is a compact handle that allows fast comparison while
also providing a generation counter to detect use-after-free or stale
references.
Key points:
- Upper 16 bits: generation - to differentiate entities at the same slot across different lifetimes.
- Lower bits: real index - the actual index (48 bits on 64-bit platforms)
is_null()checks if the index is a special null value (NULL_INDEX).to_null()replaces the real index withNULL_INDEX, preserving generation info.- 使用
generation_inc()在释放并重新分配同一槽位时递增 generation,避免 ABA 问题。
注意:真实索引必须小于或等于 INDEX_MASK,否则行为未定义;该类型提供了 compose
和 replace_index 等安全构造器/替换方法。
用例示例(简要):
let idx = GenIndex::compose(1234, 1);
let bumped = idx.generation_inc();
assert!(!idx.generation_match(bumped));Tuple Fields§
§0: u64Implementations§
Source§impl GenIndex
impl GenIndex
Sourcepub const GEN_MASK: u64 = 0xFFFF0000_00000000
pub const GEN_MASK: u64 = 0xFFFF0000_00000000
Bit mask for generation bits. ( = 0xFFFF000000000000 )
Sourcepub const INDEX_MASK: u64 = 0x0000FFFF_FFFFFFFF
pub const INDEX_MASK: u64 = 0x0000FFFF_FFFFFFFF
Bit mask for real index bits.
Sourcepub const NULL_INDEX: usize = 0x0000FFFF_FFFFFFFF
pub const NULL_INDEX: usize = 0x0000FFFF_FFFFFFFF
Special null index value for real index portion.
Sourcepub const fn compose(real_index: usize, generation: u16) -> Self
pub const fn compose(real_index: usize, generation: u16) -> Self
Compose a GenIndex from a raw real_index and generation.
构造函数:从真实索引和世代值组合出 GenIndex。real_index 会被掩码截断,
因此传入值应保证在 INDEX_MASK 范围内以避免信息丢失。
Sourcepub const fn generation(self) -> u16
pub const fn generation(self) -> u16
Return the generation (high 16 bits).
返回世代号(高 16 位)。
Sourcepub const fn real_index(self) -> usize
pub const fn real_index(self) -> usize
Return the real index (low bits masked by INDEX_MASK).
返回真实索引(低位,已掩码)。
Sourcepub const fn tear(self) -> (usize, u16)
pub const fn tear(self) -> (usize, u16)
Decompose into (real_index, generation) tuple.
拆分为 (真实索引, 世代)。
Sourcepub const fn generation_inc(self) -> Self
pub const fn generation_inc(self) -> Self
Return a new GenIndex with generation incremented by one.
世代递增(使用 wrapping add),用于在槽位复用时更新世代号以避免允许旧引用访问。
Sourcepub const fn generation_match(self, other: Self) -> bool
pub const fn generation_match(self, other: Self) -> bool
Check whether two GenIndex values have the same generation.
比较两个索引的世代是否相同(不关心真实索引部分)。
Sourcepub const fn replace_index(self, new_index: usize) -> Self
pub const fn replace_index(self, new_index: usize) -> Self
Replace the real-index portion while preserving the generation bits.
替换真实索引部分,同时保留世代位。这在需要重映射索引但保留
当前世代信息时非常有用。new_index 将被掩码截断以适配位宽。
Sourcepub const fn is_null(self) -> bool
pub const fn is_null(self) -> bool
Check whether this GenIndex represents a null sentinel index.
判定是否为特殊空索引(NULL_INDEX),通常用于表示无效/空引用。
Sourcepub const fn to_null(self) -> Self
pub const fn to_null(self) -> Self
Convert this GenIndex into a null-index while preserving generation bits.
将真实索引替换为 NULL_INDEX,保留世代信息。
Sourcepub const fn new_basic_null() -> Self
pub const fn new_basic_null() -> Self
Create a canonical null GenIndex with generation 0.
返回一个基本的空索引(世代 = 0)。