pub struct SlotMap<'a, T: Pod + FixedLayout> { /* private fields */ }Expand description
Fixed-slot map overlaid on a byte slice.
- O(1) insert (into free slot), O(1) remove, O(1) access by SlotKey.
- Generation counters prevent ABA bugs.
- Used for registries, entity systems, order books with stable handles.
Implementations§
Source§impl<'a, T: Pod + FixedLayout> SlotMap<'a, T>
impl<'a, T: Pod + FixedLayout> SlotMap<'a, T>
Sourcepub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
Overlay a SlotMap on a mutable byte slice.
Sourcepub fn insert(&mut self, value: T) -> Result<SlotKey, ProgramError>
pub fn insert(&mut self, value: T) -> Result<SlotKey, ProgramError>
Insert a value, returning a SlotKey handle.
Scans for the first free slot. O(capacity) worst case.
Sourcepub fn get(&self, key: SlotKey) -> Result<T, ProgramError>
pub fn get(&self, key: SlotKey) -> Result<T, ProgramError>
Get a value by key. Returns error if generation doesn’t match.
Sourcepub fn remove(&mut self, key: SlotKey) -> Result<T, ProgramError>
pub fn remove(&mut self, key: SlotKey) -> Result<T, ProgramError>
Remove a value by key. Bumps the generation counter.
Sourcepub const fn required_bytes(capacity: usize) -> usize
pub const fn required_bytes(capacity: usize) -> usize
Compute the byte size needed for a SlotMap with the given capacity.
Auto Trait Implementations§
impl<'a, T> Freeze for SlotMap<'a, T>
impl<'a, T> RefUnwindSafe for SlotMap<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for SlotMap<'a, T>where
T: Send,
impl<'a, T> Sync for SlotMap<'a, T>where
T: Sync,
impl<'a, T> Unpin for SlotMap<'a, T>where
T: Unpin,
impl<'a, T> UnsafeUnpin for SlotMap<'a, T>
impl<'a, T> !UnwindSafe for SlotMap<'a, T>
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