pub struct OwnedMap<K: HashWork, V, W: Work = Memory> { /* private fields */ }Expand description
A hash map whose backing allocation and operations share one resource domain.
Hashbrown supplies bucket/probing/SIMD machinery. Reservations include its control bytes and alignment, not merely the initialized entries. Hashing and actual collision comparisons are admitted before examining candidate keys.
The table’s storage belongs to its account, not to the execution W, so
OwnedMap::into_owned and OwnedMap::with_execution move a finished
table between executions without copying or rehashing it.
Implementations§
Source§impl<K: HashWork, V> OwnedMap<K, V>
impl<K: HashWork, V> OwnedMap<K, V>
Sourcepub fn with_execution<W: Work>(
self,
execution: &W,
) -> Result<OwnedMap<K, V, W>, Self>
pub fn with_execution<W: Work>( self, execution: &W, ) -> Result<OwnedMap<K, V, W>, Self>
Temporarily attaches the current operation’s work ceilings.
The execution must use this table’s existing account. On mismatch the
original map is returned unchanged. On success the table, entries,
capacity, charge and stored key metadata move without copying,
rehashing, allocation or new work. Previously admitted work is neither
reset nor charged again. Later OwnedMap::try_insert growth is paid
by execution; reads keep using the execution passed to each call.
Call OwnedMap::into_owned to retain the table independently again.
§Examples
use std::sync::Arc;
use structured_asn1::{Parser, memory::{Memory, MemoryAccount}};
let memory = Memory::new(Arc::new(MemoryAccount::new(4096, 4096)));
let mut map = memory.map();
map.try_insert(1usize, 10).unwrap();
let other = Memory::new(Arc::new(MemoryAccount::new(4096, 4096)));
let map = map.with_execution(&other).unwrap_err();
let parser = Parser::ber(memory.clone());
let reader = parser.read(&[], 0).unwrap();
let mut scoped = map.with_execution(&reader.execution()).unwrap();
scoped.try_insert(2, 20).unwrap();
let map = scoped.into_owned();
assert_eq!(map.get(&2, &memory).unwrap(), Some(&20));Source§impl<K: HashWork, V, W: Work> OwnedMap<K, V, W>
impl<K: HashWork, V, W: Work> OwnedMap<K, V, W>
Sourcepub fn into_owned(self) -> OwnedMap<K, V>
pub fn into_owned(self) -> OwnedMap<K, V>
Finishes bounded construction as an independently owned table.
Transfers the existing table and its original account without copying
entries, rehashing, reallocating, changing capacity or reserving again.
Subsequent growth uses that aggregate account; the parser-local ceiling
governed construction and is no longer attached. Borrows in K and V
retain their own lifetimes.
§Examples
use std::sync::Arc;
use structured_asn1::{Parser, memory::{Memory, MemoryAccount, Work}};
let memory = Memory::new(Arc::new(MemoryAccount::new(4096, 4096)));
let result;
{
let parser = Parser::ber(memory.clone());
let reader = parser.read(&[], 0)?;
let mut scratch = reader.execution().map();
scratch.try_insert(7usize, "seven").unwrap();
result = scratch.into_owned();
}
assert_eq!(result.get(&7, &memory).unwrap(), Some(&"seven"));
drop(result);
assert_eq!(memory.used(), 0);Sourcepub fn try_iter<'a, E: Work>(
&'a self,
execution: &E,
) -> Result<impl Iterator<Item = (&'a K, &'a V)> + 'a + use<'a, K, V, W, E>, MemoryError>
pub fn try_iter<'a, E: Work>( &'a self, execution: &E, ) -> Result<impl Iterator<Item = (&'a K, &'a V)> + 'a + use<'a, K, V, W, E>, MemoryError>
Borrowed entries after admitting a full control-bucket traversal in the current execution domain, independently of retained storage. Sparse holes count too. Partial consumption retains the conservative prepayment; the opaque iterator exposes no cloning/restart operation.
§Examples
let mut map = memory.map();
map.try_insert(1usize, 42)?;
let mut entries = map.try_iter(&memory)?;
assert_eq!(entries.next(), Some((&1, &42)));Sourcepub fn try_retain<E>(
&mut self,
execution: &impl Work,
predicate: impl FnMut(&K, &mut V) -> Result<bool, E>,
) -> Result<(), RetainError<E>>
pub fn try_retain<E>( &mut self, execution: &impl Work, predicate: impl FnMut(&K, &mut V) -> Result<bool, E>, ) -> Result<(), RetainError<E>>
Retains entries selected by a fallible predicate, without allocating.
The supplied current execution admits the entire control-bucket scan, including sparse holes, before the first predicate call. Every erase separately admits its metadata work before removal. The predicate must admit its own data-dependent work; arbitrary key/value destructors are likewise caller-owned code. No key hashing or equality is performed.
Entries are visited in unspecified order. On the first refusal no more predicates or key-work metadata callbacks run; the engine finishes its already admitted scan while retaining all remaining entries. Earlier removals and predicate value edits, including an edit made by a failing predicate, remain. Refusal never reports a successful partial filter.
The original allocation, capacity charge and storage account remain unchanged. Future growth still uses the map’s original execution. Filtering under another execution does not rebind that allocator.
§Examples
use structured_asn1::memory::{Memory, MemoryAccount};
let memory = Memory::new(Arc::new(MemoryAccount::new(1024, 1024)));
let mut map = memory.map();
map.try_insert(1usize, 10).unwrap();
map.try_insert(2usize, 20).unwrap();
map.try_retain(&memory, |key, value| {
*value += 1;
Ok::<_, core::convert::Infallible>(*key == 2)
}).unwrap();
assert_eq!(map.len(), 1);
assert_eq!(map.get(&2, &memory).unwrap(), Some(&21));Sourcepub fn get(
&self,
key: &K,
execution: &impl Work,
) -> Result<Option<&V>, MemoryError>
pub fn get( &self, key: &K, execution: &impl Work, ) -> Result<Option<&V>, MemoryError>
Looks up a key after admitting its complete possible probe work. The current execution domain pays independently of stored capacity.
§Examples
assert_eq!(map.get(&1, &memory)?, Some(&42));
assert_eq!(map.get(&2, &memory)?, None);Sourcepub fn get_key_value_borrowed<Q: HashWork + ?Sized>(
&self,
key: &Q,
execution: &impl Work,
) -> Result<Option<(&K, &V)>, MemoryError>where
K: Borrow<Q>,
pub fn get_key_value_borrowed<Q: HashWork + ?Sized>(
&self,
key: &Q,
execution: &impl Work,
) -> Result<Option<(&K, &V)>, MemoryError>where
K: Borrow<Q>,
Finds the canonical stored key and value using an allocation-free query.
Owned and borrowed forms must have identical Hash/Eq semantics, as
required by Borrow. Current work admits query hashing, table probes
and every candidate comparison before executing them. The stored key’s
HashWork bound must also cover its Borrow<Q> operation, which must
not allocate. A refusal returns Err, never successful absence or a
synthetic match. No table/account is rebound and no query key is copied.
Returning the canonical key lets an interner clone its existing shared owner without a second lookup or a temporary owned query allocation.
use std::sync::Arc;
use structured_asn1::memory::{Memory, MemoryAccount};
let storage = Memory::new(Arc::new(MemoryAccount::new(4096, 4096)));
let mut map = storage.map();
map.try_insert(7usize, "retained")?;
let query = Memory::new(Arc::new(MemoryAccount::new(0, 1024)));
assert_eq!(map.get_key_value_borrowed(&7, &query)?, Some((&7, &"retained")));Sourcepub fn get_mut(
&mut self,
key: &K,
execution: &impl Work,
) -> Result<Option<&mut V>, MemoryError>
pub fn get_mut( &mut self, key: &K, execution: &impl Work, ) -> Result<Option<&mut V>, MemoryError>
Looks up an entry for mutation without exposing table growth. Hashing, probing and equality use the supplied execution domain.
§Examples
if let Some(value) = map.get_mut(&1, &memory)? { *value = 7; }
assert_eq!(map.get(&1, &memory)?, Some(&7));Sourcepub fn get_mut_borrowed<Q: HashWork + ?Sized>(
&mut self,
key: &Q,
execution: &impl Work,
) -> Result<Option<&mut V>, MemoryError>where
K: Borrow<Q>,
pub fn get_mut_borrowed<Q: HashWork + ?Sized>(
&mut self,
key: &Q,
execution: &impl Work,
) -> Result<Option<&mut V>, MemoryError>where
K: Borrow<Q>,
Finds a mutable value using an allocation-free borrowed query.
Uses the same Hash/Eq/Borrow and HashWork requirements as
Self::get_key_value_borrowed. Current work admits hashing, probes and
each candidate comparison before exposing a mutable value. Refusal is
Err, never absence or a synthetic match. No owned query, second lookup
or table/account rebinding is performed.
Sourcepub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, MemoryError>
pub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, MemoryError>
Inserts a value fallibly, or replaces the value for an existing key.
This storage mutation runs in the map’s attached execution, including
hashing, rehashing, allocation and copying work. Table growth admits
its storage and work before the allocator backend, in the same order
as super::OwnedVec. To mutate under another same-account execution,
attach it with OwnedMap::with_execution.
Sourcepub fn remove(
&mut self,
key: &K,
execution: &impl Work,
) -> Result<Option<V>, MemoryError>
pub fn remove( &mut self, key: &K, execution: &impl Work, ) -> Result<Option<V>, MemoryError>
Removes an entry, retaining table capacity and its storage charge. The supplied execution domain pays for lookup and removal before mutation.
§Examples
let capacity_charge = memory.used();
assert_eq!(map.remove(&1, &memory)?, Some(42));
assert_eq!(memory.used(), capacity_charge);Sourcepub fn remove_borrowed<Q: HashWork + ?Sized>(
&mut self,
key: &Q,
execution: &impl Work,
) -> Result<Option<V>, MemoryError>where
K: Borrow<Q>,
pub fn remove_borrowed<Q: HashWork + ?Sized>(
&mut self,
key: &Q,
execution: &impl Work,
) -> Result<Option<V>, MemoryError>where
K: Borrow<Q>,
Removes a value using an allocation-free borrowed query.
Uses the same Hash/Eq/Borrow and HashWork requirements as
Self::get_key_value_borrowed. Current work admits lookup and erase
metadata/value movement before mutation. Refusal leaves the entry intact.
The original table capacity and allocator remain retained; the removed
key is dropped and the value transfers its independent ownership.
Arbitrary key/value destructor work remains caller-owned.