[][src]Struct fn_cache::HashCache

pub struct HashCache<'a, I, O, V = O> where
    I: Eq + Hash
{ /* fields omitted */ }

A cache for a function which uses a HashMap.

The cache takes ownership of all inputs, but only passes a reference to the function, allowing it to store the input in the cache without any copies or clones.

The value in the cache V can be different than the output of the function O, as long as O implements Into<V>. If no conversion is required, than the V parameter can be elided.

The requirements for a HashMap must be met, specifically the keys must implement Eq and Hash, and the following propery must hold:

k1 == k2 -> hash(k1) == hash(k2)

Methods

impl<'a, I, O, V> HashCache<'a, I, O, V> where
    I: Eq + Hash,
    O: Into<V>, 
[src]

pub fn new<F>(f: F) -> Self where
    F: Fn(&mut Self, &I) -> O + 'a, 
[src]

Create a cache for the provided function. If the function stores references, the cache can only live as long as those references.

pub fn get(&mut self, input: I) -> &V[src]

Retrieve a value stored in the cache. If the value does not yet exist in the cache, the function is called, and the result is added to the cache before returning it.

Auto Trait Implementations

impl<'a, I, O, V> Unpin for HashCache<'a, I, O, V> where
    I: Unpin,
    V: Unpin

impl<'a, I, O, V = O> !Sync for HashCache<'a, I, O, V>

impl<'a, I, O, V = O> !Send for HashCache<'a, I, O, V>

impl<'a, I, O, V = O> !UnwindSafe for HashCache<'a, I, O, V>

impl<'a, I, O, V = O> !RefUnwindSafe for HashCache<'a, I, O, V>

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]