Struct Memo

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

Source

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.

Source

pub fn clear(&mut self)

Clears any cached value

The value will be reevaluated the next time it is needed.

Source

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.

Source

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.

Source

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.

Source

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().

Source

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.

Source

pub fn param(&self) -> &P

Get a reference to the parameter used for the computation

Source

pub fn param_mut(&mut self) -> &mut P

Get a mutable reference to the parameter used for the computation

This clears any cached value.

Source

pub fn update_param<F>(&mut self, op: F)
where F: FnOnce(&mut P),

Modify the parameter used for the computation

Takes a closure and applies it to the parameter.

This clears any cached value.

Trait Implementations§

Source§

impl<T: Debug + Memoize, P: Debug + Borrow<T::Param>> Debug for Memo<T, P>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, P> Freeze for Memo<T, P>
where P: Freeze, T: Freeze,

§

impl<T, P> RefUnwindSafe for Memo<T, P>

§

impl<T, P> Send for Memo<T, P>
where P: Send, T: Send,

§

impl<T, P> Sync for Memo<T, P>
where P: Sync, T: Sync,

§

impl<T, P> Unpin for Memo<T, P>
where P: Unpin, T: Unpin,

§

impl<T, P> UnwindSafe for Memo<T, P>
where P: UnwindSafe, T: UnwindSafe,

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.