Struct MemoOnce

Source
pub struct MemoOnce<'p, T: Memoize>
where T::Param: 'p,
{ /* private fields */ }
Expand description

Memoized value which holds a reference to the parameter for its computation

See the crate-level documentation for information how to use the library.

This type is designed for one-shot lazy computations. It holds a reference to the input parameter for the computation, meaning that it cannot be mutated while the MemoOnce is alive.

§Example

use core_memo::{Memoize, MemoOnce};

// something trivial for the sake of example
struct MemoLength(usize);

impl Memoize for MemoLength {
    type Param = String;

    fn memoize(p: &String) -> Self {
        MemoLength(p.len())
    }
}

let mut my_string = String::from("My length is important!");

// ... some fancy computations ...

{
    // we want to use our hard-to-compute value many times in this block,
    // so we want to memoize it:
    let mut len: MemoOnce<MemoLength> = MemoOnce::new(&my_string);

    // ... more fancy computations ...

    assert_eq!(len.get().0, 23);

    // ... more stuff ...

    println!("{}", len.get().0);
}

// now our `MemoOnce` has been dropped, our String is no longer borrowed,
// and we are free to mutate it:
my_string.push_str(" Not anymore!");

Implementations§

Source§

impl<'p, T: Memoize> MemoOnce<'p, T>

Source

pub fn new(p: &'p T::Param) -> Self

Creates a new MemoOnce instance

You must pass a reference to the object which will be used as the parameter for your computation.

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) -> &T::Param

Get a reference to the parameter used for the computation

Trait Implementations§

Source§

impl<'p, T: Debug + Memoize> Debug for MemoOnce<'p, T>
where T::Param: 'p + Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'p, T> Freeze for MemoOnce<'p, T>
where T: Freeze,

§

impl<'p, T> RefUnwindSafe for MemoOnce<'p, T>

§

impl<'p, T> Send for MemoOnce<'p, T>
where T: Send, <T as Memoize>::Param: Sync,

§

impl<'p, T> Sync for MemoOnce<'p, T>
where T: Sync, <T as Memoize>::Param: Sync,

§

impl<'p, T> Unpin for MemoOnce<'p, T>
where T: Unpin,

§

impl<'p, T> UnwindSafe for MemoOnce<'p, T>

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.