Skip to main content

OwnedMap

Struct OwnedMap 

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

Implementations§

Source§

impl<K: HashWork, V, W: Work> OwnedMap<K, V, W>

Source

pub fn memory(&self) -> &Memory

The shared account retained until the table is deallocated.

Source

pub fn len(&self) -> usize

Number of initialized entries.

Source

pub fn is_empty(&self) -> bool

Whether there are no entries.

Source

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

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

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

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")));
Source

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

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 collection’s original resource domain, including hashing, rehashing, allocation and copying work. To mutate under a different domain, build a new collection there from explicitly admitted borrowed entries; retained capacity is not rebound.

Source

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);

Trait Implementations§

Source§

impl<K: HashWork + Debug, V: Debug, W: Work> Debug for OwnedMap<K, V, W>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<K, V, W = Memory> !Freeze for OwnedMap<K, V, W>

§

impl<K, V, W> RefUnwindSafe for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: RefUnwindSafe, W: RefUnwindSafe,

§

impl<K, V, W> Send for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: Send, W: Send,

§

impl<K, V, W> Sync for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: Sync, W: Sync,

§

impl<K, V, W> Unpin for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: Unpin, W: Unpin,

§

impl<K, V, W> UnsafeUnpin for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: UnsafeUnpin, W: UnsafeUnpin,

§

impl<K, V, W> UnwindSafe for OwnedMap<K, V, W>
where HashMap<K, V, DefaultHashBuilder, AccountAllocator<W>>: UnwindSafe, W: 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.