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 fromIndex<Bar>. This prevents accidentally using an index from a pool ofFoos to access a pool ofBars. ThePhantomData<T>marker enforces this at compile time with zero runtime cost. - “None” State: The maximum value of
u32is reserved forIndex::NONE, creating a clear, efficient “null” or “invalid” state, similar toOption<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>
impl<T> Index<T>
Sourcepub const NONE: Self
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.
Trait Implementations§
Source§impl<T> From<usize> for Index<T>
impl<T> From<usize> for Index<T>
Source§fn from(index: usize) -> Index<T>
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());impl<T> Copy for Index<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more