pub struct KeyedCache<F, K, V>where
    F: FnMut(&K) -> V,
    K: Hash + Eq,{ /* private fields */ }
Expand description

Caches the result of an (presumably) expensive operation such that accessing the result multiple times doesn’t result in running the expensive operation multiple times.

Implementations§

source§

impl<F, K, V> KeyedCache<F, K, V>where F: FnMut(&K) -> V, K: Hash + Eq,

source

pub fn new(calculation_fn: F) -> Self

Creates a new KeyedCache<F, V> initialized with the given calculation_fn function. The function will not be called until the result of a calculation is needed.

source

pub fn value_mut(&mut self, key: K) -> &mut V

Gets a mutable reference to a contained calculated value based on the key. Runs the calculation function if this method call is the first time the value with the given key is accessed.

source

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

Gets a shared reference to the contained calculated value based on the key if it has already been calculated.

Trait Implementations§

source§

impl<F, K, V: Debug> Debug for KeyedCache<F, K, V>where F: FnMut(&K) -> V + Debug, K: Hash + Eq + Debug,

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<F, K, V> RefUnwindSafe for KeyedCache<F, K, V>where F: RefUnwindSafe, K: RefUnwindSafe, V: RefUnwindSafe,

§

impl<F, K, V> Send for KeyedCache<F, K, V>where F: Send, K: Send, V: Send,

§

impl<F, K, V> Sync for KeyedCache<F, K, V>where F: Sync, K: Sync, V: Sync,

§

impl<F, K, V> Unpin for KeyedCache<F, K, V>where F: Unpin, K: Unpin, V: Unpin,

§

impl<F, K, V> UnwindSafe for KeyedCache<F, K, V>where F: UnwindSafe, K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> MapAny for T

source§

fn map<U, F>(self, map_fn: F) -> Uwhere F: Fn(T) -> U,

Takes a closure and calls it with Self, then returns whatever the closure returned.
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.