Skip to main content

PackedMap

Struct PackedMap 

Source
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>

Source

pub fn from_bytes(data: &'a mut [u8]) -> Result<Self, ProgramError>

Overlay a PackedMap on a mutable byte slice.

Source

pub fn len(&self) -> usize

Current number of entries.

Source

pub fn capacity(&self) -> usize

Maximum capacity.

Source

pub fn is_empty(&self) -> bool

Is the map empty?

Source

pub fn is_full(&self) -> bool

Is the map full?

Source

pub fn find(&self, key: &K) -> Option<usize>

Find the index of a key, or None.

Source

pub fn get(&self, key: &K) -> Result<V, ProgramError>

Get the value for a key, or error if not found.

Source

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.

Source

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.

Source

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.

Source

pub fn contains(&self, key: &K) -> bool

Check if the map contains a key.

Source

pub fn clear(&mut self)

Clear all entries.

Source

pub fn iter(&self) -> PackedMapIter<'_, K, V>

Iterate over all entries (key, value) by index.

Returns an iterator that yields (K, V) copies.

Auto Trait Implementations§

§

impl<'a, K, V> Freeze for PackedMap<'a, K, V>

§

impl<'a, K, V> RefUnwindSafe for PackedMap<'a, K, V>

§

impl<'a, K, V> Send for PackedMap<'a, K, V>
where K: Send, V: Send,

§

impl<'a, K, V> Sync for PackedMap<'a, K, V>
where K: Sync, V: Sync,

§

impl<'a, K, V> Unpin for PackedMap<'a, K, V>
where K: Unpin, V: Unpin,

§

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> 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> 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, 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.