Struct State

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

Source

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

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

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

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

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

Source

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

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

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

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

impl<T> State<T>
where T: Clone + Display + 'static,

Source

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

Creates a lazy string memo using ToString::to_string.

§Examples
use ailloli_ui_runtime::component::State;
assert_eq!(State::new(12).to_text().read(), "12");

Trait Implementations§

Source§

impl<T> Clone for State<T>

Implements the Clone contract for State<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<State<T>> for Binding<T>

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

Source§

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

Performs the documented infallible conversion.

Source§

impl<T> From<State<T>> for Signal<T>

Implements the From<State<T>> contract for Signal<T>.

Source§

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

Performs the documented infallible conversion.

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> 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,