Struct HashCache

Source
pub struct HashCache<'f, I, O, S = RandomState>
where I: Eq + Hash, S: BuildHasher,
{ /* 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: impl Fn(&I) -> O + Send + 'f) -> Self

Source

pub fn recursive( f: impl Fn(&mut RefCache<'_, HashMap<I, O>>, &I) -> O + Send + 'f, ) -> Self

Source§

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

Source

pub fn with_hasher(hash_builder: S, f: impl Fn(&I) -> O + Send + 'f) -> Self

Source

pub fn recursive_with_hasher( hash_builder: S, f: impl Fn(&mut RefCache<'_, HashMap<I, O, S>>, &I) -> O + Send + 'f, ) -> Self

Methods from Deref<Target = GenericCache<'f, HashMap<I, O, S>>>§

Source

pub fn cache(&self) -> &C

Get a reference to the underlying cache object, letting you use functions exclusive to the cache type (as long they only need &self of course).

Source

pub fn len(&self) -> usize

Returns the number of elements in the cache.

Source

pub fn clear(&mut self)

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

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: &C::Input) -> Option<C::Output>

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> Deref for HashCache<'f, I, O, S>
where I: Eq + Hash, S: BuildHasher,

Source§

type Target = GenericCache<'f, HashMap<I, O, S>>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

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

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

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

Source§

fn from(value: GenericCache<'f, HashMap<I, O, S>>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

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

§

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 S: Send, I: Send, O: Send,

§

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

§

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

§

impl<'f, I, O, S = RandomState> !UnwindSafe for HashCache<'f, I, O, 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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.