weak_table

Struct WeakKeyHashMap

Source
pub struct WeakKeyHashMap<K, V, S = RandomState> { /* private fields */ }
Expand description

A hash map with weak keys, hashed on key value.

When a weak pointer expires, its mapping is lazily removed.

Implementations§

Source§

impl<K: WeakKey, V> WeakKeyHashMap<K, V, RandomState>

Source

pub fn new() -> Self

Creates an empty WeakKeyHashMap.

O(1) time

Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty WeakKeyHashMap with the given capacity.

O(n) time

Source§

impl<K: WeakKey, V, S: BuildHasher> WeakKeyHashMap<K, V, S>

Source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty WeakKeyHashMap with the given capacity and hasher.

O(n) time

Source

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

Creates an empty WeakKeyHashMap with the given capacity and hasher.

O(n) time

Source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

O(1) time

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating.

O(1) time

Source

pub fn remove_expired(&mut self)

Removes all mappings whose keys have expired.

O(n) time

Source

pub fn reserve(&mut self, additional_capacity: usize)

Reserves room for additional elements.

O(n) time

Source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity to the minimum allowed to hold the current number of elements.

O(n) time

Source

pub fn len(&self) -> usize

Returns an over-approximation of the number of elements.

O(1) time

Source

pub fn is_empty(&self) -> bool

Is the map empty?

Note that this may return false even if all keys in the map have expired, if they haven’t been collected yet.

O(1) time

Source

pub fn load_factor(&self) -> f32

The proportion of buckets that are used.

This is an over-approximation because of expired keys.

O(1) time

Source

pub fn entry(&mut self, key: K::Strong) -> Entry<'_, K, V>

Gets the requested entry.

expected O(1) time; worst-case O(p) time

Source

pub fn clear(&mut self)

Removes all associations from the map.

O(n) time

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns a reference to the value corresponding to the key.

expected O(1) time; worst-case O(p) time

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns true if the map contains the specified key.

expected O(1) time; worst-case O(p) time

Source

pub fn get_key<Q>(&self, key: &Q) -> Option<K::Strong>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns a strong reference to the key, if found.

expected O(1) time; worst-case O(p) time

Source

pub fn get_both<Q>(&self, key: &Q) -> Option<(K::Strong, &V)>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns a pair of a strong reference to the key, and a reference to the value, if present.

expected O(1) time; worst-case O(p) time

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns a mutable reference to the value corresponding to the key.

expected O(1) time; worst-case O(p) time

Source

pub fn get_both_mut<Q>(&mut self, key: &Q) -> Option<(K::Strong, &mut V)>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Returns a pair of a strong reference to the key, and a mutable reference to the value, if present.

expected O(1) time; worst-case O(p) time

Source

pub fn insert(&mut self, key: K::Strong, value: V) -> Option<V>

Unconditionally inserts the value, returning the old value if already present.

Unlike std::collections::HashMap, this replaced the key even if occupied.

expected O(1) time; worst-case O(p) time

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where Q: ?Sized + Hash + Eq, K::Key: Borrow<Q>,

Removes the entry with the given key, if it exists, and returns the value.

expected O(1) time; worst-case O(p) time

Source

pub fn retain<F>(&mut self, f: F)
where F: FnMut(K::Strong, &mut V) -> bool,

Removes all mappings not satisfying the given predicate.

Also removes any expired mappings.

O(n) time

Source

pub fn is_submap_with<F, S1, V1>( &self, other: &WeakKeyHashMap<K, V1, S1>, cmp: F, ) -> bool
where F: FnMut(&V, &V1) -> bool, S1: BuildHasher,

Is this map a submap of the other under the given value comparison cmp?

In particular, for every key k of self,

  • k must also be a key of other and
  • cmp(self[k], other[k]) must hold.

expected O(n) time; worst-case O(nq) time (where n is self.capacity() and q is the length of the probe sequences in other)

Source

pub fn is_submap<V1, S1>(&self, other: &WeakKeyHashMap<K, V1, S1>) -> bool
where V: PartialEq<V1>, S1: BuildHasher,

Is self a submap of other?

expected O(n) time; worst-case O(nq) time (where n is self.capacity() and q is the length of the probe sequences in other)

Source

pub fn domain_is_subset<V1, S1>( &self, other: &WeakKeyHashMap<K, V1, S1>, ) -> bool
where S1: BuildHasher,

Are the keys of self a subset of the keys of other?

expected O(n) time; worst-case O(nq) time (where n is self.capacity() and q is the length of the probe sequences in other)

Source§

impl<K: WeakElement, V, S> WeakKeyHashMap<K, V, S>

Source

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

Gets an iterator over the keys and values.

O(1) time

Source

pub fn keys(&self) -> Keys<'_, K, V>

Gets an iterator over the keys.

O(1) time

Source

pub fn values(&self) -> Values<'_, K, V>

Gets an iterator over the values.

O(1) time

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

Gets an iterator over the keys and mutable values.

O(1) time

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>

Gets an iterator over the mutable values.

O(1) time

Source

pub fn drain(&mut self) -> Drain<'_, K, V>

Gets a draining iterator, which removes all the values but retains the storage.

O(1) time (and O(n) time to dispose of the result)

Trait Implementations§

Source§

impl<K: Clone, V: Clone, S: Clone> Clone for WeakKeyHashMap<K, V, S>

Source§

fn clone(&self) -> WeakKeyHashMap<K, V, S>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: WeakElement, V: Debug, S> Debug for WeakKeyHashMap<K, V, S>
where K::Strong: Debug,

Source§

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

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

impl<K: WeakKey, V, S: BuildHasher + Default> Default for WeakKeyHashMap<K, V, S>

Source§

fn default() -> Self

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

impl<'a, K, V, S> Extend<(&'a <K as WeakElement>::Strong, &'a V)> for WeakKeyHashMap<K, V, S>
where K: 'a + WeakKey, K::Strong: Clone, V: 'a + Clone, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = (&'a K::Strong, &'a V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S> Extend<(<K as WeakElement>::Strong, V)> for WeakKeyHashMap<K, V, S>
where K: WeakKey, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = (K::Strong, V)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, S> FromIterator<(<K as WeakElement>::Strong, V)> for WeakKeyHashMap<K, V, S>
where K: WeakKey, S: BuildHasher + Default,

Source§

fn from_iter<T: IntoIterator<Item = (K::Strong, V)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, K, V, S, Q> Index<&'a Q> for WeakKeyHashMap<K, V, S>
where K: WeakKey, K::Key: Borrow<Q>, S: BuildHasher, Q: ?Sized + Eq + Hash,

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, index: &'a Q) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K, V, S, Q> IndexMut<&'a Q> for WeakKeyHashMap<K, V, S>
where K: WeakKey, K::Key: Borrow<Q>, S: BuildHasher, Q: ?Sized + Eq + Hash,

Source§

fn index_mut(&mut self, index: &'a Q) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, K: WeakElement, V, S> IntoIterator for &'a WeakKeyHashMap<K, V, S>

Source§

fn into_iter(self) -> Self::IntoIter

Creates a borrowing iterator from self.

O(1) time

Source§

type Item = (<K as WeakElement>::Strong, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

impl<'a, K: WeakElement, V, S> IntoIterator for &'a mut WeakKeyHashMap<K, V, S>

Source§

fn into_iter(self) -> Self::IntoIter

Creates a borrowing iterator from self.

O(1) time

Source§

type Item = (<K as WeakElement>::Strong, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

impl<K: WeakElement, V, S> IntoIterator for WeakKeyHashMap<K, V, S>

Source§

fn into_iter(self) -> Self::IntoIter

Creates an owning iterator from self.

O(1) time (and O(n) time to dispose of the result)

Source§

type Item = (<K as WeakElement>::Strong, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

impl<K, V, V1, S, S1> PartialEq<WeakKeyHashMap<K, V1, S1>> for WeakKeyHashMap<K, V, S>
where K: WeakKey, V: PartialEq<V1>, S: BuildHasher, S1: BuildHasher,

Source§

fn eq(&self, other: &WeakKeyHashMap<K, V1, S1>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K: WeakKey, V: Eq, S: BuildHasher> Eq for WeakKeyHashMap<K, V, S>

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<K, V, S> Sync for WeakKeyHashMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

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

§

impl<K, V, S> UnwindSafe for WeakKeyHashMap<K, V, S>
where S: UnwindSafe, K: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.