Struct VecCache

Source
pub struct VecCache<'f, O> { /* private fields */ }
Expand description

A cache for a function which uses a Vec.

This cache is optimized for functions which must be calculated in order, so that there can be no gaps in the cache, and use usize as an argument.

If the function does not start at zero, or require every previous value to be calculated for the next one, consider using a HashCache instead.

Implementations§

Source§

impl<'f, O> VecCache<'f, O>

Source

pub fn new<F>(f: F) -> Self
where F: Fn(&usize) -> 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) -> Self
where F: Fn(&mut Self, &usize) -> 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

pub fn clear(&mut self)

Clears the cache. removing all values. 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.

Trait Implementations§

Source§

impl<'f, O> FnCache<usize, O> for VecCache<'f, O>

Source§

fn get(&mut self, input: usize) -> &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, O> FnCacheMany<usize, O> for VecCache<'f, O>

Source§

fn get_many<const N: usize>(&mut self, inputs: [usize; 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, O> Freeze for VecCache<'f, O>

§

impl<'f, O> !RefUnwindSafe for VecCache<'f, O>

§

impl<'f, O> Send for VecCache<'f, O>
where O: Send,

§

impl<'f, O> Sync for VecCache<'f, O>
where O: Sync,

§

impl<'f, O> Unpin for VecCache<'f, O>
where O: Unpin,

§

impl<'f, O> !UnwindSafe for VecCache<'f, O>

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

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.