Struct Memo

Source
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>.

Source

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);
Source

pub fn read(&self) -> T

Evaluates and returns the current derived value.

§Panics

Any panic from the stored computation propagates to the caller.

§Examples
use ailloli_ui_runtime::component::Memo;
let memo = Memo::new(|| String::from("fresh"));
assert_eq!(memo.read(), "fresh");
Source

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);
Source

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);
Source§

impl<T> Memo<T>
where T: Display + 'static,

Source

pub fn to_text(&self) -> Memo<String>

Lazily formats each source value through ToString::to_string.

§Examples
use ailloli_ui_runtime::component::Memo;
assert_eq!(Memo::new(|| 8).to_text().read(), "8");
Source§

impl<T> Memo<T>
where T: 'static,

Source

pub fn to_text_with<F>(&self, format: F) -> Memo<String>
where F: Fn(T) -> String + 'static,

Lazily formats each source value with format.

§Examples
use ailloli_ui_runtime::component::Memo;
let text = Memo::new(|| 8).to_text_with(|value| format!("{value}px"));
assert_eq!(text.read(), "8px");

Trait Implementations§

Source§

impl<T> Clone for Memo<T>

Implements the Clone contract for Memo<T>.

Source§

fn clone(&self) -> Self

Produces the clone required by the standard cloning contract.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> From<Memo<T>> for Binding<T>

Implements the From<Memo<T>> contract for Binding<T>.

Source§

fn from(value: Memo<T>) -> Self

Performs the documented infallible conversion.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<A> IntoClickAction<A> for A
where A: Clone + 'static,

Source§

fn into_click_action(self) -> ClickAction<A>

Implements the into_click_action helper used by this module.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Props for T
where T: Clone + 'static,