Struct Memo

Source
pub struct Memo<T, F>
where F: Fn() -> T,
{ /* private fields */ }
Expand description

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

Memo<T, F> 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.
  • F: The closure type that computes the value. Must implement Fn() -> T.

Implementations§

Source§

impl<T, F> Memo<T, F>
where F: Fn() -> T,

Source

pub fn new(f: F) -> Self

Creates a new Memo wrapping the provided closure.

§Examples
use reactive_cache::Memo;

let memo = Memo::new(|| 10);
Source

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

Returns the memoized value, recomputing it only if necessary.

During the computation, dependencies are tracked for reactive updates.

§Examples
use once_cell::unsync::Lazy;
use reactive_cache::Memo;

static mut MEMO: Lazy<Memo<i32, fn() -> i32>> = Lazy::new(|| Memo::new(|| 5));
assert_eq!(unsafe { (*MEMO).get() }, 5);

Auto Trait Implementations§

§

impl<T, F> !Freeze for Memo<T, F>

§

impl<T, F> !RefUnwindSafe for Memo<T, F>

§

impl<T, F> !Send for Memo<T, F>

§

impl<T, F> !Sync for Memo<T, F>

§

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

§

impl<T, F> !UnwindSafe for Memo<T, F>

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.