pub struct PackedMap<'a, K: Pod + FixedLayout + PartialEq, V: Pod + FixedLayout> { /* private fields */ }Expand description
Packed key->value map overlaid on a byte slice.
Supports O(n) lookup, O(1) insert (append), O(1) remove (swap-last). No heap allocation.
Implementations§
Source§impl<'a, K: Pod + FixedLayout + PartialEq, V: Pod + FixedLayout> PackedMap<'a, K, V>
impl<'a, K: Pod + FixedLayout + PartialEq, V: Pod + FixedLayout> PackedMap<'a, K, V>
Sourcepub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>
Overlay a PackedMap on a mutable byte slice.
Sourcepub fn get(&self, key: &K) -> Result<V, ProgramError>
pub fn get(&self, key: &K) -> Result<V, ProgramError>
Get the value for a key, or error if not found.
Sourcepub fn get_value_offset(&self, key: &K) -> Result<usize, ProgramError>
pub fn get_value_offset(&self, key: &K) -> Result<usize, ProgramError>
Get a mutable reference to the value bytes for a key. Returns the byte offset of the value within data for direct manipulation.
Sourcepub fn insert(&mut self, key: K, value: V) -> Result<bool, ProgramError>
pub fn insert(&mut self, key: K, value: V) -> Result<bool, ProgramError>
Insert or update a key-value pair.
If the key already exists, updates the value and returns true.
If the key is new, appends and returns false.
Returns error if full and key doesn’t exist.
Sourcepub fn remove(&mut self, key: &K) -> Result<V, ProgramError>
pub fn remove(&mut self, key: &K) -> Result<V, ProgramError>
Remove a key-value pair by swapping with the last entry. O(1).
Returns the removed value, or error if key not found.
Auto Trait Implementations§
impl<'a, K, V> Freeze for PackedMap<'a, K, V>
impl<'a, K, V> RefUnwindSafe for PackedMap<'a, K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<'a, K, V> Send for PackedMap<'a, K, V>
impl<'a, K, V> Sync for PackedMap<'a, K, V>
impl<'a, K, V> Unpin for PackedMap<'a, K, V>
impl<'a, K, V> UnsafeUnpin for PackedMap<'a, K, V>
impl<'a, K, V> !UnwindSafe for PackedMap<'a, K, V>
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