Skip to main content

GenIndex

Struct GenIndex 

Source
#[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 with NULL_INDEX, preserving generation info.
  • 使用 generation_inc() 在释放并重新分配同一槽位时递增 generation,避免 ABA 问题。

注意:真实索引必须小于或等于 INDEX_MASK,否则行为未定义;该类型提供了 composereplace_index 等安全构造器/替换方法。

用例示例(简要):

let idx = GenIndex::compose(1234, 1);
let bumped = idx.generation_inc();
assert!(!idx.generation_match(bumped));

Tuple Fields§

§0: u64

Implementations§

Source§

impl GenIndex

Source

pub const GEN_SHIFT: u32 = 48

Bit shift amount for generation bits. ( = 48 )

Source

pub const GEN_MASK: u64 = 0xFFFF0000_00000000

Bit mask for generation bits. ( = 0xFFFF000000000000 )

Source

pub const INDEX_MASK: u64 = 0x0000FFFF_FFFFFFFF

Bit mask for real index bits.

Source

pub const NULL_INDEX: usize = 0x0000FFFF_FFFFFFFF

Special null index value for real index portion.

Source

pub const fn compose(real_index: usize, generation: u16) -> Self

Compose a GenIndex from a raw real_index and generation.

构造函数:从真实索引和世代值组合出 GenIndexreal_index 会被掩码截断, 因此传入值应保证在 INDEX_MASK 范围内以避免信息丢失。

Source

pub const fn generation(self) -> u16

Return the generation (high 16 bits).

返回世代号(高 16 位)。

Source

pub const fn real_index(self) -> usize

Return the real index (low bits masked by INDEX_MASK).

返回真实索引(低位,已掩码)。

Source

pub const fn tear(self) -> (usize, u16)

Decompose into (real_index, generation) tuple.

拆分为 (真实索引, 世代)

Source

pub const fn generation_inc(self) -> Self

Return a new GenIndex with generation incremented by one.

世代递增(使用 wrapping add),用于在槽位复用时更新世代号以避免允许旧引用访问。

Source

pub const fn generation_match(self, other: Self) -> bool

Check whether two GenIndex values have the same generation.

比较两个索引的世代是否相同(不关心真实索引部分)。

Source

pub const fn replace_index(self, new_index: usize) -> Self

Replace the real-index portion while preserving the generation bits.

替换真实索引部分,同时保留世代位。这在需要重映射索引但保留 当前世代信息时非常有用。new_index 将被掩码截断以适配位宽。

Source

pub const fn is_null(self) -> bool

Check whether this GenIndex represents a null sentinel index.

判定是否为特殊空索引(NULL_INDEX),通常用于表示无效/空引用。

Source

pub const fn to_null(self) -> Self

Convert this GenIndex into a null-index while preserving generation bits.

将真实索引替换为 NULL_INDEX,保留世代信息。

Source

pub const fn new_basic_null() -> Self

Create a canonical null GenIndex with generation 0.

返回一个基本的空索引(世代 = 0)。

Trait Implementations§

Source§

impl Clone for GenIndex

Source§

fn clone(&self) -> GenIndex

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GenIndex

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E, P> From<GenIndex> for IndexedID<E, P>

Source§

fn from(indexed: GenIndex) -> Self

Converts to this type from the input type.
Source§

impl From<GenIndex> for u64

Source§

fn from(value: GenIndex) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for GenIndex

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl Hash for GenIndex

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for GenIndex

Source§

fn cmp(&self, other: &GenIndex) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for GenIndex

Source§

fn eq(&self, other: &GenIndex) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for GenIndex

Source§

fn partial_cmp(&self, other: &GenIndex) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Copy for GenIndex

Source§

impl Eq for GenIndex

Source§

impl StructuralPartialEq for GenIndex

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.