weak_table::traits

Trait WeakKey

Source
pub trait WeakKey: WeakElement {
    type Key: ?Sized + Eq + Hash;

    // Required method
    fn with_key<F, R>(view: &Self::Strong, f: F) -> R
       where F: FnOnce(&Self::Key) -> R;

    // Provided methods
    fn hash<H: Hasher>(view: &Self::Strong, h: &mut H) { ... }
    fn equals<Q>(view: &Self::Strong, key: &Q) -> bool
       where Q: ?Sized + Eq,
             Self::Key: Borrow<Q> { ... }
}
Expand description

Interface for elements that can act as keys in weak hash tables.

To use an element as a weak hash map key or weak hash set element), the hash table needs to be able to view the actual key values to hash and compare them. This trait provides the necessary mechanism.

Required Associated Types§

Source

type Key: ?Sized + Eq + Hash

The underlying key type.

For example, for std::rc::Weak<T>, this will be T.

Required Methods§

Source

fn with_key<F, R>(view: &Self::Strong, f: F) -> R
where F: FnOnce(&Self::Key) -> R,

Allows borrowing a view of the key, via a callback.

Rather than returning a borrowed reference to the actual key, this method passes a reference to the key to a callback with an implicit higher-order lifetime bound. This is necessary to get the lifetimes right in cases where the key is not actually store in the strong pointer.

Provided Methods§

Source

fn hash<H: Hasher>(view: &Self::Strong, h: &mut H)

Hashes the key view into the given Hasher.

Source

fn equals<Q>(view: &Self::Strong, key: &Q) -> bool
where Q: ?Sized + Eq, Self::Key: Borrow<Q>,

Returns whether the key view equals the given key.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T: ?Sized + Eq + Hash> WeakKey for Weak<T>

Source§

type Key = T

Source§

fn with_key<F, R>(view: &Self::Strong, f: F) -> R
where F: FnOnce(&Self::Key) -> R,

Source§

impl<T: ?Sized + Eq + Hash> WeakKey for Weak<T>

Source§

type Key = T

Source§

fn with_key<F, R>(view: &Self::Strong, f: F) -> R
where F: FnOnce(&Self::Key) -> R,

Implementors§