pub struct Cacher<F, U, V>{ /* private fields */ }
Expand description
The Cacher struct (Memoization) stores a function and a Hashmap. The HashMap keeps track of previous input and output for the function so that it only ever has to be called once per input. Use for expensive functions.
Implementations§
Source§impl<F, U, V> Cacher<F, U, V>
impl<F, U, V> Cacher<F, U, V>
Sourcepub fn call(&mut self, arg: U) -> V
pub fn call(&mut self, arg: U) -> V
Performs a lookup into the HashMap to see if the value has already been calculated. If it has, returns the value. If it has not, calls the function, stores the value, then returns the value.
§Examples
let mut squared = Cacher::new(|n: u32| n*n);
// This is where we call the function
let sixteen = squared.call(4);
Sourcepub fn call_and_replace(&mut self, arg: U) -> V
pub fn call_and_replace(&mut self, arg: U) -> V
Calls the function without performing a lookup and replaces the old return value with the new one, and returns it. Potentially useful if the function reads from a file or RNG whose state may have changed.
Auto Trait Implementations§
impl<F, U, V> Freeze for Cacher<F, U, V>where
F: Freeze,
impl<F, U, V> RefUnwindSafe for Cacher<F, U, V>
impl<F, U, V> Send for Cacher<F, U, V>
impl<F, U, V> Sync for Cacher<F, U, V>
impl<F, U, V> Unpin for Cacher<F, U, V>
impl<F, U, V> UnwindSafe for Cacher<F, U, V>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more