Index

Struct Index 

Source
pub struct Index<T> { /* private fields */ }
Expand description

A type-safe, compact handle to an element in an ElemPool.

An Index<T> is essentially a wrapper around a u32, providing a cheap, Copy-able way to reference list elements without relying on raw pointers or garbage collection.

§Rationale

Using a custom Index type instead of a raw usize or u32 provides several benefits:

  • Type Safety: Index<Foo> is a different type from Index<Bar>. This prevents accidentally using an index from a pool of Foos to access a pool of Bars. The PhantomData<T> marker enforces this at compile time with zero runtime cost.
  • “None” State: The maximum value of u32 is reserved for Index::NONE, creating a clear, efficient “null” or “invalid” state, similar to Option<T> but without the added size overhead.
  • API Clarity: Using Index<T> in function signatures makes it clear that the function expects a handle to a list element, not just an arbitrary number.

Implementations§

Source§

impl<T> Index<T>

Source

pub const NONE: Self

An invalid index, conceptually similar to Option::None.

This constant value (u32::MAX) is reserved to represent a null or sentinel link. All valid indices into the ElemPool will be less than this value.

Source

pub fn is_some(&self) -> bool

Returns true if the index is valid (i.e., not Index::NONE).

§Example
let valid_index = Index::<i32>::from(10_u32);
assert!(valid_index.is_some());

let invalid_index = Index::<i32>::NONE;
assert!(!invalid_index.is_some());
Source

pub fn is_none(&self) -> bool

Returns true if the index is invalid (i.e., it is Index::NONE).

§Example
let invalid_index = Index::<i32>::NONE;
assert!(invalid_index.is_none());

let valid_index = Index::<i32>::from(10_u32);
assert!(!valid_index.is_none());

Trait Implementations§

Source§

impl<T> Clone for Index<T>

Source§

fn clone(&self) -> Self

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<T> Debug for Index<T>

Source§

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

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

impl<T> Default for Index<T>

Source§

fn default() -> Self

The default Index is Index::NONE.

Source§

impl<T> Display for Index<T>

Source§

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

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

impl<T> From<u32> for Index<T>

Source§

fn from(ndx: u32) -> Index<T>

Creates an Index from a raw u32.

Source§

impl<T> From<usize> for Index<T>

Source§

fn from(index: usize) -> Index<T>

Creates an Index from a usize.

This conversion is fallible. Values greater than or equal to u32::MAX will be converted into an invalid index (Index::NONE). This prevents out-of-bounds errors when converting from usize on 64-bit platforms.

§Example
let index1 = Index::<char>::from(123_usize);
assert!(index1.is_some());

let index2 = Index::<char>::from(u32::MAX as usize);
assert!(index2.is_none());
Source§

impl<T> Hash for Index<T>

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<T> PartialEq for Index<T>

Source§

fn eq(&self, other: &Self) -> 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<T> Copy for Index<T>

Source§

impl<T: Eq> Eq for Index<T>

Auto Trait Implementations§

§

impl<T> Freeze for Index<T>

§

impl<T> RefUnwindSafe for Index<T>
where T: RefUnwindSafe,

§

impl<T> Send for Index<T>
where T: Send,

§

impl<T> Sync for Index<T>
where T: Sync,

§

impl<T> Unpin for Index<T>
where T: Unpin,

§

impl<T> UnwindSafe for Index<T>
where T: UnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.