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 implementClone
.F
: The closure type that computes the value. Must implementFn() -> T
.
Implementations§
Source§impl<T, F> Memo<T, F>where
F: Fn() -> T,
impl<T, F> Memo<T, F>where
F: Fn() -> T,
Sourcepub fn new(f: F) -> Self
pub fn new(f: F) -> Self
Creates a new Memo
wrapping the provided closure.
§Examples
use reactive_cache::Memo;
let memo = Memo::new(|| 10);
Sourcepub fn get(&'static self) -> Twhere
T: Clone,
pub fn get(&'static self) -> Twhere
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more