Struct OverlayMap

Source
pub struct OverlayMap<K, V, S = RandomState>
where K: Eq + Hash,
{ /* private fields */ }
Expand description

A two-layered map where each key has a mutable foreground and an optional background value.

When inserting a new value for a key, the previous value (if any) is automatically moved to the background. Background values are preserved but not cloned.

This map is not thread-safe for mutation. It may be shared across threads for read-only access.

Implementations§

Source§

impl<K, V, S> OverlayMap<K, V, S>
where K: Eq + Hash, S: BuildHasher + Default,

Source

pub fn new() -> Self

Creates a new, empty OverlayMap with the default hasher.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty OverlayMap with the specified capacity and default hasher.

Source

pub fn with_hasher(hasher: S) -> Self

Creates an empty OverlayMap that will use the given hasher.

Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self

Creates an empty OverlayMap with the specified capacity and hasher.

Source

pub fn len(&self) -> usize

Number of unique keys in the map.

Source

pub fn is_empty(&self) -> bool

Check if the map is empty.

Source

pub fn fg(&self, key: &K) -> Option<&V>

Get an immutable reference to the value associated with the key.

Returns None if the key was not found in the map.

Source

pub fn bg(&self, key: &K) -> Option<&V>

Get an immutable reference to the value associated with the key in the background layer.

Returns None if the key was not found in the background layer.

Source

pub fn push(&mut self, key: K, value: V) -> bool

Push a value into the foreground layer, preserving the previous value in the background.

If the key was already present, the current foreground is moved to the background slot, and the new value becomes the new foreground. No cloning occurs. The old background value is dropped if it was present.

Returns true if there was already a foreground value (i.e. a background now exists).

Source

pub fn push_if<F>(&mut self, key: &K, predicate: F) -> bool
where F: FnOnce(&V) -> Option<V>,

Conditionally push a new value into the foreground based on the current value.

If the key exists, the current foreground value is passed to the predicate. If the predicate returns Some(new_val), the new value is pushed and the old one is preserved in the background. If it returns None, nothing is changed.

Returns true if a new value was pushed.

Source

pub fn overlay<I>(&mut self, iter: I) -> usize
where I: IntoIterator<Item = (K, V)>,

Overlay multiple values onto the map.

Each key-value pair is pushed into the foreground layer. If the key was already present, the existing foreground value is moved to the background. This does not clone or retain old values beyond the background layer.

Returns the number of keys that already existed (i.e. pushes that replaced a foreground).

Trait Implementations§

Source§

impl<K, V: Debug, S: Debug> Debug for OverlayMap<K, V, S>
where K: Eq + Hash + Debug,

Source§

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

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

impl<K, V: Default, S: Default> Default for OverlayMap<K, V, S>
where K: Eq + Hash + Default,

Source§

fn default() -> OverlayMap<K, V, S>

Returns the “default value” for a type. Read more
Source§

impl<K, V, S> Sync for OverlayMap<K, V, S>
where K: Eq + Hash + Sync, S: Sync,

Auto Trait Implementations§

§

impl<K, V, S> Freeze for OverlayMap<K, V, S>
where S: Freeze,

§

impl<K, V, S> RefUnwindSafe for OverlayMap<K, V, S>

§

impl<K, V, S> Send for OverlayMap<K, V, S>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Unpin for OverlayMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for OverlayMap<K, V, S>
where K: UnwindSafe, S: UnwindSafe, V: 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> 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.