Struct core_memo::MemoExt [−][src]
pub struct MemoExt<T: Memoize> { /* fields omitted */ }
Memoized value with a parameter provided externally
See the crate-level documentation for information how to use the library.
This is the simplest memoization type, but the trickiest to use.
This type does not keep track of the parameter for the computation and
requires you to provide it externally with every call to get()
.
You need to make sure to manually call clear()
to invalidate the cached
output whenever you need it to be reevaluated (typically after mutating
the input parameter). You probably also want to make sure that you provide
the same parameter every time you call get()
.
It is very easy to introduce logic bugs in your program with this type. You
should prefer the Memo
or MemoOnce
types, unless you need the extra
flexibility provided by MemoExt
.
Example
use core_memo::{Memoize, MemoExt}; // something trivial for the sake of example struct CopyInt(i32); impl Memoize for CopyInt { type Param = i32; fn memoize(p: &i32) -> Self { CopyInt(*p) } } // notice we do not provide a param to `new()`: let mut memo: MemoExt<CopyInt> = MemoExt::new(); let param = 420; // we have to manually provide the param with each call to get assert_eq!(memo.get(¶m).0, 420); let meaning_of_life = 42; // we have the freedom to do whatever we want with the param and // provide anything to the `MemoExt`: println!("The meaning of life is {}.", memo.get(&meaning_of_life).0); // ^ WHOOPS: This actually prints "The meaning of life is 420." :D // This is because we haven't called `clear()` to invalidate the previous // cached value! This could be a bug in your program! memo.clear(); // now our computation will be reevaluated: assert_eq!(memo.get(&meaning_of_life).0, 42);
Methods
impl<T: Memoize> MemoExt<T>
[src]
impl<T: Memoize> MemoExt<T>
pub fn new() -> Self
[src]
pub fn new() -> Self
Creates a new MemoExt
instance
pub fn clear(&mut self)
[src]
pub fn clear(&mut self)
Clears any cached value
You must call this whenever it is invalid.
The value will be reevaluated the next time it is needed.
pub fn is_ready(&self) -> bool
[src]
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.
pub fn ready(&mut self, p: &T::Param)
[src]
pub fn ready(&mut self, p: &T::Param)
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.
pub fn update(&mut self, p: &T::Param)
[src]
pub fn update(&mut self, p: &T::Param)
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.
pub fn get(&mut self, p: &T::Param) -> &T
[src]
pub fn get(&mut self, p: &T::Param) -> &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()
.
pub fn try_get(&self) -> Option<&T>
[src]
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
.