Memo

Struct Memo 

Source
pub struct Memo<T> { /* private fields */ }
Expand description

A memoized reactive computation that caches its result and tracks dependencies.

Memo<T> behaves similarly to a computed property: it stores the result of a closure and only recomputes when its dependencies change. Other signals or effects that access the memo will automatically be tracked.

In short:

  • Like a computed property: returns a cached value derived from other signals.
  • Adds tracking: recomputes only when dependencies are invalidated.

§Type Parameters

  • T: The result type of the computation. Must implement Clone.

§Examples

§Basic usage

use std::rc::Rc;
use reactive_cache::{Signal, Memo};

let counter = Signal::new(1);
let double = {
    let counter = Rc::clone(&counter);
    Memo::new({
        let counter = Rc::new(counter);
        move || *counter.get() * 2
    })
};

assert_eq!(double.get(), 2);
counter.set(3);
assert_eq!(double.get(), 6);

§Using inside a struct

use std::rc::Rc;
use reactive_cache::{Signal, Memo};

struct ViewModel {
    counter: Rc<Signal<i32>>,
    double: Rc<Memo<i32>>,
}

let counter = Signal::new(1);
let double = Memo::new({
    let counter = counter.clone();
    move || *counter.get() * 2
});

let vm = ViewModel { counter, double };
assert_eq!(vm.double.get(), 2);
vm.counter.set(4);
assert_eq!(vm.double.get(), 8);

Implementations§

Source§

impl<T> Memo<T>

Source

pub fn new(f: impl Fn() -> T + 'static) -> Rc<Self>
where T: 'static,

Creates a new Memo wrapping the provided closure.

§Requirements
  • T must be 'static, because the value is stored in global cache.
  • The closure must be 'static as well.
§Examples

Basic usage:

use reactive_cache::Memo;

let memo = Memo::new(|| 10);
assert_eq!(memo.get(), 10);

Using inside a struct:

use std::rc::Rc;

use reactive_cache::{Signal, Memo};

struct ViewModel {
    a: Rc<Signal<i32>>,
    b: Rc<Signal<i32>>,
    sum: Rc<Memo<i32>>,
}

// Construct signals
let a = Signal::new(2);
let b = Signal::new(3);

// Construct a memo depending on `a` and `b`
let sum = {
    let a = a.clone();
    let b = b.clone();
    Memo::new(move || {
        // `Signal::get()` will register dependencies automatically
        *a.get() + *b.get()
    })
};

let vm = ViewModel { a, b, sum };

// Initial computation
assert_eq!(vm.sum.get(), 5);

// Update a signal → memo recomputes
vm.a.set(10);
assert_eq!(vm.sum.get(), 13);
Source

pub fn get(&self) -> T
where T: Clone + 'static,

Returns the memoized value, recomputing it only if necessary.

During the computation, dependencies are tracked for reactive updates.

§Examples
use reactive_cache::Memo;

let memo = Memo::new(|| 5);
assert_eq!(memo.get(), 5);

Auto Trait Implementations§

§

impl<T> !Freeze for Memo<T>

§

impl<T> !RefUnwindSafe for Memo<T>

§

impl<T> !Send for Memo<T>

§

impl<T> !Sync for Memo<T>

§

impl<T> Unpin for Memo<T>

§

impl<T> !UnwindSafe for Memo<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.