pub struct OverlayMap<K, V, S = RandomState>{ /* 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>
impl<K, V, S> OverlayMap<K, V, S>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty OverlayMap with the specified capacity and default hasher.
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Creates an empty OverlayMap that will use the given hasher.
Sourcepub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self
Creates an empty OverlayMap with the specified capacity and hasher.
Sourcepub fn fg(&self, key: &K) -> Option<&V>
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.
Sourcepub fn bg(&self, key: &K) -> Option<&V>
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.
Sourcepub fn push(&mut self, key: K, value: V) -> bool
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).
Sourcepub fn push_if<F>(&mut self, key: &K, predicate: F) -> bool
pub fn push_if<F>(&mut self, key: &K, predicate: F) -> bool
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.
Sourcepub fn overlay<I>(&mut self, iter: I) -> usizewhere
I: IntoIterator<Item = (K, V)>,
pub fn overlay<I>(&mut self, iter: I) -> usizewhere
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).