pub struct Memo<T> { /* private fields */ }Expand description
Derived read-only value computed from signals or other memos.
A memo stores functions, not a cached value. Every Self::read evaluates
its closure again. Clones share the same closures through Rc and are
neither Send nor Sync. Revision reporting is advisory: Self::new
always reports zero, while memos derived through Signal::map propagate
their source revision.
A memo is dynamically reactive when its computation reads a Signal or
super::State during a retained build, layout, or paint callback, even if
its advisory revision is zero. Only sources reached by the executed branch
are observed. After the surrounding callback succeeds, that conditional set
atomically replaces the callback’s previous observations; a panic leaves the
previous set active. Reads outside retained callbacks are passive.
§Examples
use std::{cell::Cell, rc::Rc};
use ailloli_ui_runtime::component::Memo;
let calls = Rc::new(Cell::new(0));
let observed = calls.clone();
let memo = Memo::new(move || { observed.set(observed.get() + 1); 7 });
assert_eq!((memo.read(), memo.read()), (7, 7));
assert_eq!(calls.get(), 2);Implementations§
Source§impl<T> Memo<T>
Provides the operations defined for Memo<T>.
impl<T> Memo<T>
Provides the operations defined for Memo<T>.
Sourcepub fn new(read_fn: impl Fn() -> T + 'static) -> Self
pub fn new(read_fn: impl Fn() -> T + 'static) -> Self
Creates an opaque lazy computation with constant revision zero.
read_fn is not evaluated during construction and its outputs are not
cached. If it reads a signal during a retained callback, that signal is
observed dynamically even though Self::revision remains zero. Other
mutable external state can change without notifying the retained runtime.
§Examples
use ailloli_ui_runtime::component::Memo;
let memo = Memo::new(|| 21 * 2);
assert_eq!(memo.read(), 42);
assert_eq!(memo.revision(), 0);Sourcepub fn revision(&self) -> u64
pub fn revision(&self) -> u64
Evaluates the memo’s advisory revision function.
A standalone memo returns zero. Derived signal memos return the source’s nonzero-after-change wrapping revision. This call does not read the value, but a signal-derived revision function observes that signal when invoked during a retained callback.
§Examples
use ailloli_ui_runtime::component::Memo;
let memo = Memo::new(|| 1);
assert_eq!(memo.revision(), 0);Sourcepub fn map<U: 'static>(&self, f: impl Fn(T) -> U + 'static) -> Memo<U>where
T: 'static,
pub fn map<U: 'static>(&self, f: impl Fn(T) -> U + 'static) -> Memo<U>where
T: 'static,
Lazily maps this memo while preserving its revision function.
Neither the source nor result is cached; each result read first evaluates
the source and then f.
§Examples
use ailloli_ui_runtime::component::Memo;
let source = Memo::new(|| 5);
let label = source.map(|value| format!("value={value}"));
assert_eq!(label.read(), "value=5");
assert_eq!(label.revision(), 0);Trait Implementations§
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> 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<A> IntoClickAction<A> for Awhere
A: Clone + 'static,
impl<A> IntoClickAction<A> for Awhere
A: Clone + 'static,
Source§fn into_click_action(self) -> ClickAction<A>
fn into_click_action(self) -> ClickAction<A>
Implements the into_click_action helper used by this module.