pub struct Memo<T: Memoize, P: Borrow<T::Param> = <T as Memoize>::Param> { /* private fields */ }
Expand description
Memoized value which holds ownership over the parameter for its computation
See the crate-level documentation for information how to use the library.
This type holds ownership over the input parameter to your computation. It
keeps everything nicely together and is the safest to use. If this is too
restrictive for you, consider using MemoExt
instead.
You can modify the parameter using param_mut()
or update_param()
. Any
cached value will be cleared and will be recomputed on the next access.
§Example
See the crate-level documentation for an example.
Implementations§
Source§impl<T: Memoize, P: Borrow<T::Param>> Memo<T, P>
impl<T: Memoize, P: Borrow<T::Param>> Memo<T, P>
Sourcepub fn new(p: P) -> Self
pub fn new(p: P) -> Self
Creates a new Memo
instance
You must pass in the object which will be used as the parameter
for your computation. The Memo
will take ownership over it.
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears any cached value
The value will be reevaluated the next time it is needed.
Sourcepub fn is_ready(&self) -> bool
pub fn is_ready(&self) -> bool
Check if there is a cached value
If this method returns true
, the next call to get()
will return a
stored memoized value.
If this method returns false
, the next call to get()
will recompute
the value.
Sourcepub fn ready(&mut self)
pub fn ready(&mut self)
If the value is not ready, compute it and cache it
Call this method if you want to make sure that future get()
calls can
return instantly without computing the value.
Sourcepub fn update(&mut self)
pub fn update(&mut self)
Force the value to be recomputed
This discards any stored value and computes a new one immediately.
It is probably better to call clear()
instead, to compute the value
lazily when it is next needed.
Sourcepub fn get(&mut self) -> &T
pub fn get(&mut self) -> &T
Get the value
If the value has already been computed, this function returns the cached value. If not, it is computed and cached for future use.
If you need to make sure this method always returns quickly, call
ready()
beforehand or use try_get()
.
Sourcepub fn try_get(&self) -> Option<&T>
pub fn try_get(&self) -> Option<&T>
Get the value if it is available
If there is a cached value, returns it. If the value needs to be
computed, returns None
.
Sourcepub fn param_mut(&mut self) -> &mut P
pub fn param_mut(&mut self) -> &mut P
Get a mutable reference to the parameter used for the computation
This clears any cached value.
Sourcepub fn update_param<F>(&mut self, op: F)
pub fn update_param<F>(&mut self, op: F)
Modify the parameter used for the computation
Takes a closure and applies it to the parameter.
This clears any cached value.