pub struct State<T> { /* private fields */ }Expand description
Public DX state handle built on top of the retained runtime Signal.
Clones alias one signal value, revision, historical callback, and dynamic
retained consumers. Like Signal, this Rc-based type is UI-thread-local
and neither Send nor Sync. Self::new starts without an owner callback,
but every successful read during retained Build, Layout, or Paint registers
that exact mounted consumer. Reads outside those callbacks remain passive.
Successful callbacks replace their previous observations atomically, so
conditional reads track only the branch that committed. A failed callback
preserves its previous observations. Unmounting removes the consumer; the
state itself remains usable while another handle owns it.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(1);
let alias = state.clone();
alias.set(2);
assert_eq!(state.read(), 2);Implementations§
Source§impl<T: 'static> State<T>
Provides the operations defined for State<T>.
impl<T: 'static> State<T>
Provides the operations defined for State<T>.
Sourcepub fn new(value: T) -> Self
pub fn new(value: T) -> Self
Creates standalone state without an initial owner callback.
Mutations advance the hidden source revision and notify retained consumers established by later Build/Layout/Paint reads. Before such a read, after their unmount, or when accessed only outside retained scopes, the state schedules no frame. This differs from state created by a runtime context, which also retains its owner-provided historical invalidator for compatibility.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(String::from("ready"));
assert_eq!(state.read(), "ready");Sourcepub fn from_signal(signal: Signal<T>) -> Self
pub fn from_signal(signal: Signal<T>) -> Self
Wraps an existing signal while preserving its shared invalidator/revision.
§Examples
use std::{cell::RefCell, rc::Rc};
use ailloli_ui_runtime::component::{Signal, State};
let signal = Signal::new(Rc::new(RefCell::new(1)), Rc::new(|| {}));
let state = State::from_signal(signal.clone());
state.set(2);
assert_eq!((signal.read(), signal.revision()), (2, 1));Sourcepub fn set(&self, value: T)
pub fn set(&self, value: T)
Replaces the value and notifies the underlying signal’s consumers.
Equality is not checked. Borrowing and callback panics follow
Signal::set.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(1);
state.set(4);
assert_eq!(state.read(), 4);Sourcepub fn update(&self, f: impl FnOnce(&mut T))
pub fn update(&self, f: impl FnOnce(&mut T))
Mutates the value in place and notifies the underlying signal’s consumers.
The closure is executed synchronously under a mutable RefCell borrow;
panic behavior follows Signal::update.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(vec![1]);
state.update(|items| items.push(2));
assert_eq!(state.read(), vec![1, 2]);Sourcepub fn into_signal(self) -> Signal<T>
pub fn into_signal(self) -> Signal<T>
Consumes this handle and returns its underlying signal.
Other cloned state handles continue to alias the returned signal.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(3);
let alias = state.clone();
let signal = state.into_signal();
signal.set(4);
assert_eq!(alias.read(), 4);Source§impl<T: Clone + 'static> State<T>
Provides the operations defined for State<T>.
impl<T: Clone + 'static> State<T>
Provides the operations defined for State<T>.
Sourcepub fn read(&self) -> T
pub fn read(&self) -> T
Clones, observes, and returns the current value.
§Panics
Panics if cloning T panics or the underlying signal value is already
mutably borrowed through another alias.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(String::from("copy"));
let value: String = state.read();
assert_eq!(value, "copy");Sourcepub fn map<U: 'static>(&self, f: impl Fn(T) -> U + 'static) -> Memo<U>
pub fn map<U: 'static>(&self, f: impl Fn(T) -> U + 'static) -> Memo<U>
Creates a non-caching lazy memo with this state’s source revision.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(2);
let squared = state.map(|value| value * value);
assert_eq!(squared.read(), 4);
state.set(3);
assert_eq!(squared.read(), 9);Sourcepub fn to_text_with<F>(&self, format: F) -> Memo<String>
pub fn to_text_with<F>(&self, format: F) -> Memo<String>
Creates a lazy string memo using a custom formatter.
§Examples
use ailloli_ui_runtime::component::State;
let state = State::new(12);
assert_eq!(state.to_text_with(|value| format!("{value}%")).read(), "12%");Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for State<T>
impl<T> !RefUnwindSafe for State<T>
impl<T> !Send for State<T>
impl<T> !Sync for State<T>
impl<T> Unpin for State<T>
impl<T> !UnwindSafe for State<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.