Struct fn_cache::HashCache

source ·
pub struct HashCache<'f, I, O, S = RandomState>where
    I: Eq + Hash,{ /* private fields */ }
Expand description

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

Implementations§

source§

impl<'f, I, O> HashCache<'f, I, O, RandomState>where I: Eq + Hash,

source

pub fn new<F>(f: F) -> Selfwhere F: Fn(&I) -> O + 'f + Send + Sync,

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

source

pub fn recursive<F>(f: F) -> Selfwhere F: Fn(&mut Self, &I) -> O + 'f + Send + Sync,

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

source§

impl<'f, I, O, S> HashCache<'f, I, O, S>where I: Eq + Hash, S: BuildHasher,

source

pub fn with_hasher<F>(hash_builder: S, f: F) -> Selfwhere F: Fn(&I) -> O + 'f + Send + Sync,

Create a HashCache which will use the given hash builder to hash keys.

See the documentation on HashMap::with_hasher for more details.

source

pub fn recursive_with_hasher<F>(hash_builder: S, f: F) -> Selfwhere F: Fn(&mut Self, &I) -> O + 'f + Send + Sync,

Create a recursive HashCache which will use the given hash builder to hash keys.

See the documentation on HashMap::with_hasher for more details.

source

pub fn clear(&mut self)

Clears the cache, removing all key-value pairs. Keeps the allocated memory for reuse.

source

pub fn len(&self) -> usize

Returns the number of elements in the cache.

source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the cache. The collection may reserve more space to avoid frequent reallocations.

source

pub fn remove(&mut self, input: &I) -> Option<O>

Removes the input from the cache, returning any value if the input was previously in the cache.

Trait Implementations§

source§

impl<'f, I, O, S> FnCache<I, O> for HashCache<'f, I, O, S>where I: Eq + Hash, S: BuildHasher,

source§

fn get(&mut self, input: I) -> &O

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.
source§

impl<'f, I, O, S> FnCacheMany<I, O> for HashCache<'f, I, O, S>where I: Eq + Hash + Clone, S: BuildHasher,

source§

fn get_many<const N: usize>(&mut self, inputs: [I; N]) -> [&O; N]

Retrieve multiple values stored in the cache. If any of the values do not yet exist, the function is called, and the result is added to the cache before returning them. Read more

Auto Trait Implementations§

§

impl<'f, I, O, S = RandomState> !RefUnwindSafe for HashCache<'f, I, O, S>

§

impl<'f, I, O, S> Send for HashCache<'f, I, O, S>where I: Send, O: Send, S: Send,

§

impl<'f, I, O, S> Sync for HashCache<'f, I, O, S>where I: Sync, O: Sync, S: Sync,

§

impl<'f, I, O, S> Unpin for HashCache<'f, I, O, S>where I: Unpin, O: Unpin, S: Unpin,

§

impl<'f, I, O, S = RandomState> !UnwindSafe for HashCache<'f, I, O, S>

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