Struct WeakMap

Source
pub struct WeakMap<K, V> { /* private fields */ }
Expand description

A B-Tree map that stores weak references to values.

Implementations§

Source§

impl<K, V> WeakMap<K, V>

Source

pub const fn new() -> Self

Makes a new, empty WeakMap.

Does not allocate anything on its own.

Source§

impl<K, V> WeakMap<K, V>

Source

pub fn clear(&mut self)

Clears the map, removing all elements.

Source

pub fn raw_len(&self) -> usize

Returns the number of elements in the underlying map.

Source

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

Gets an iterator over the entries of the map, sorted by key.

Source

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

Gets an iterator over the keys of the map, in sorted order.

Source

pub fn into_keys(self) -> IntoKeys<K, V>

Creates a consuming iterator visiting all the keys, in sorted order. The map cannot be used after calling this.

Source

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

Gets an iterator over the values of the map, in order by key.

Source

pub fn into_values(self) -> IntoValues<K, V>

Creates a consuming iterator visiting all the values, in order by key. The map cannot be used after calling this.

Source§

impl<K, V> WeakMap<K, V>
where K: Ord, V: WeakRef,

Source

pub fn cleanup(&mut self)

Cleans up the map by removing expired values.

Usually you don’t need to call this manually, as it is called automatically when the number of operations reaches a threshold.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map, excluding expired values.

This is a linear operation, as it iterates over all elements in the map.

The returned value may be less than the result of Self::raw_len.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no valid elements.

Source

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

Retains only the elements specified by the predicate.

Source

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

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, V::Strong)>
where K: Borrow<Q>, Q: Ord + ?Sized,

Returns the key-value pair corresponding to the supplied key. This is potentially useful:

  • for key types where non-identical keys can be considered equal;
  • for getting the &K stored key value from a borrowed &Q lookup key; or
  • for getting a reference to a key with the same lifetime as the collection.

The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

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

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

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

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the module-level documentation for more.

Source

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

Removes a key from the map, returning the value at the key if the key was previously in the map.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V::Strong)>
where K: Borrow<Q>, Q: Ord + ?Sized,

Removes a key from the map, returning the stored key and value if the key was previously in the map.

The key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.

Source

pub fn upgrade(&self) -> StrongMap<K, V::Strong>
where K: Clone,

Upgrade this WeakMap to a StrongMap.

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for WeakMap<K, V>

Source§

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

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

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

Performs copy-assignment from source. Read more
Source§

impl<K, V> Debug for WeakMap<K, V>
where K: Debug, V: WeakRef, V::Strong: Debug,

Source§

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

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

impl<K, V> Default for WeakMap<K, V>

Source§

fn default() -> Self

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

impl<K, V> From<&BTreeMap<K, <V as WeakRef>::Strong>> for WeakMap<K, V>
where K: Ord + Clone, V: WeakRef,

Source§

fn from(value: &StrongMap<K, V::Strong>) -> Self

Converts to this type from the input type.
Source§

impl<K, V, const N: usize> From<[(K, &<V as WeakRef>::Strong); N]> for WeakMap<K, V>
where K: Ord, V: WeakRef,

Source§

fn from(array: [(K, &V::Strong); N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<BTreeMap<K, V>> for WeakMap<K, V>

Source§

fn from(inner: BTreeMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<K, V> From<WeakMap<K, V>> for BTreeMap<K, V>

Source§

fn from(map: WeakMap<K, V>) -> Self

Converts to this type from the input type.
Source§

impl<'a, K, V> FromIterator<(K, &'a <V as WeakRef>::Strong)> for WeakMap<K, V>
where K: Ord, V: WeakRef,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K, V> IntoIterator for &'a WeakMap<K, V>
where V: WeakRef,

Source§

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

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

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

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> IntoIterator for WeakMap<K, V>
where V: WeakRef,

Source§

type IntoIter = IntoIter<K, V>

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

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

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V> PartialEq for WeakMap<K, V>
where K: Ord, V: WeakRef,

Source§

fn eq(&self, other: &Self) -> bool

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

const 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, V> Eq for WeakMap<K, V>
where K: Ord, V: WeakRef,

Auto Trait Implementations§

§

impl<K, V> !Freeze for WeakMap<K, V>

§

impl<K, V> RefUnwindSafe for WeakMap<K, V>

§

impl<K, V> Send for WeakMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for WeakMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for WeakMap<K, V>

§

impl<K, V> UnwindSafe for WeakMap<K, V>

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.