Struct cachemap2::CacheMap

source ·
pub struct CacheMap<K, V, S = RandomState> { /* private fields */ }
Expand description

An insert-only map for caching the result of functions

Implementations§

source§

impl<K: Eq + Hash, V, S: BuildHasher> CacheMap<K, V, S>

source

pub fn cache<F: FnOnce() -> V>(&self, key: K, f: F) -> &V

Fetch the value associated with the key, or run the provided function to insert one.

Example
use cachemap2::CacheMap;

let m = CacheMap::new();

let fst = m.cache("key", || 5u32);
let snd = m.cache("key", || 7u32);

assert_eq!(*fst, *snd);
assert_eq!(*fst, 5u32);
source

pub fn cache_default(&self, key: K) -> &V
where V: Default,

Fetch the value associated with the key, or insert a default value.

source

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

Return whether the map contains the given key.

source

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

Return an iterator over the map.

This iterator will lock the underlying map until it is dropped.

source§

impl<K: Eq + Hash, V> CacheMap<K, V, RandomState>

source

pub fn new() -> Self

Creates a new CacheMap

source§

impl<K: Eq + Hash, V, S: BuildHasher + Default> CacheMap<K, V, S>

source

pub fn with_hasher(hash_builder: S) -> Self

Creates a new CacheMap with the provided hasher

Trait Implementations§

source§

impl<K: Eq + Hash, V, S: BuildHasher + Default> Default for CacheMap<K, V, S>

source§

fn default() -> Self

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

impl<K: Eq + Hash, V, S: BuildHasher + Default> FromIterator<(K, V)> for CacheMap<K, V, S>

source§

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

Creates a value from an iterator. Read more
source§

impl<'a, K, V, S> IntoIterator for &'a CacheMap<K, V, S>

§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
§

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

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<K, V, S> IntoIterator for CacheMap<K, V, S>

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = IntoIter<K, V>

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

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<K, V, S> Sync for CacheMap<K, V, S>
where K: Send, S: Send, V: Send,

§

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

§

impl<K, V, S> UnwindSafe for CacheMap<K, V, S>

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>,

§

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>,

§

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.