pub struct Cage<T: 'static> { /* private fields */ }Expand description
Copy, thread-local reactive identity. get returns an owned clone; use
with for large collections so indexed reads never clone the whole data set.
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let value = owner.cage(1);
std::thread::spawn(move || value.get()); // Handles cannot cross UI threads.Implementations§
Source§impl<T: 'static> Cage<Vec<T>>
impl<T: 'static> Cage<Vec<T>>
Sourcepub fn item(self, index: usize) -> Cage<T>
pub fn item(self, index: usize) -> Cage<T>
Track a Vec position, including a currently missing one, without copying
its element. Insert/remove/reorder retarget this position. For stable
business identity, keep order separately and use a keyed map’s entry.
try_with/try_revise return MissingField while the position is absent.
pub fn try_item(self, index: usize) -> Result<Cage<T>, AccessError>
Source§impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>
impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>
Sourcepub fn entry(self, key: K) -> Cage<V>
pub fn entry(self, key: K) -> Cage<V>
Track a map key without cloning the map or its value. The same path
observes MissingField after removal and the replacement after reinsert;
use a generation in business keys when replacement means a new entity.
Keys need not be Clone; Hash/Eq/Ord must be pure. Arbitrary revise
notifies all live entries; insert/remove and entry writes leave
unrelated keys alone.
use std::collections::BTreeMap;
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let rows = owner.cage(BTreeMap::from([(7, String::from("row"))]));
let row = rows.entry(7);
row.revise(|value| value.push('!'));
assert_eq!(row.get(), "row!");
rows.revise(|rows| { rows.remove(&7); });
assert_eq!(row.try_get(), Err(glory_core::AccessError::MissingField));pub fn try_entry(self, key: K) -> Result<Cage<V>, AccessError>
Source§impl<T: 'static> Cage<Vec<T>>
impl<T: 'static> Cage<Vec<T>>
Sourcepub fn push(self, value: T)
pub fn push(self, value: T)
Append without notifying old positions. No element/collection Clone.
try_ reports reactive access/index/length errors; allocation and user
destructor panics have the same behavior as ordinary Vec operations.
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let rows = owner.cage(Vec::<usize>::new());
let first = rows.item(0);
rows.push(7);
rows.insert(0, 9);
assert_eq!(first.get(), 9);
assert_eq!(rows.remove(0), 9);
assert_eq!(first.get(), 7);pub fn try_push(self, value: T) -> Result<(), AccessError>
pub fn pop(self) -> Option<T>
pub fn try_pop(self) -> Result<Option<T>, AccessError>
Sourcepub fn insert(self, index: usize, value: T)
pub fn insert(self, index: usize, value: T)
Insert at a position; only registered positions in [index, old_len] change. Positions are not stable entity identities; use map keys for that.
pub fn try_insert(self, index: usize, value: T) -> Result<(), AccessError>
pub fn remove(self, index: usize) -> T
pub fn try_remove(self, index: usize) -> Result<T, AccessError>
pub fn truncate(self, len: usize)
pub fn try_truncate(self, len: usize) -> Result<(), AccessError>
pub fn clear(self)
pub fn try_clear(self) -> Result<(), AccessError>
Source§impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>
impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>
Sourcepub fn try_update<R>(
self,
key: &K,
edit: impl FnOnce(&mut V) -> R,
) -> Result<R, AccessError>
pub fn try_update<R>( self, key: &K, edit: impl FnOnce(&mut V) -> R, ) -> Result<R, AccessError>
Edit one present key without constructing a persistent entry alias. Only that key’s registered fields and map/ancestor readers publish.
Sourcepub fn insert(self, key: K, value: V) -> Option<V>
pub fn insert(self, key: K, value: V) -> Option<V>
Insert/replace one key. Publishes the map/ancestors and that key’s live aliases/children; unrelated keys are not invalidated. Keys and values need not be Clone. Hash/Eq/Ord/hasher must be pure.
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let rows = owner.cage(std::collections::HashMap::<u64, String>::new());
let title = rows.entry(7);
assert_eq!(title.try_get(), Err(glory_core::AccessError::MissingField));
rows.insert(7, String::from("ready"));
assert_eq!(title.get(), "ready");
assert_eq!(rows.remove(&7).as_deref(), Some("ready"));pub fn try_insert(self, key: K, value: V) -> Result<Option<V>, AccessError>
Sourcepub fn remove(self, key: &K) -> Option<V>
pub fn remove(self, key: &K) -> Option<V>
Missing-key removal is a no-op (no versions, work or wake).
pub fn try_remove(self, key: &K) -> Result<Option<V>, AccessError>
Source§impl<K: Eq + Hash + 'static, V: 'static, S: BuildHasher + 'static> Cage<HashMap<K, V, S>>
impl<K: Eq + Hash + 'static, V: 'static, S: BuildHasher + 'static> Cage<HashMap<K, V, S>>
Sourcepub fn try_update<R>(
self,
key: &K,
edit: impl FnOnce(&mut V) -> R,
) -> Result<R, AccessError>
pub fn try_update<R>( self, key: &K, edit: impl FnOnce(&mut V) -> R, ) -> Result<R, AccessError>
Edit one present key without constructing a persistent entry alias. Only that key’s registered fields and map/ancestor readers publish.
Sourcepub fn insert(self, key: K, value: V) -> Option<V>
pub fn insert(self, key: K, value: V) -> Option<V>
Insert/replace one key. Publishes the map/ancestors and that key’s live aliases/children; unrelated keys are not invalidated. Keys and values need not be Clone. Hash/Eq/Ord/hasher must be pure.
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let rows = owner.cage(std::collections::HashMap::<u64, String>::new());
let title = rows.entry(7);
assert_eq!(title.try_get(), Err(glory_core::AccessError::MissingField));
rows.insert(7, String::from("ready"));
assert_eq!(title.get(), "ready");
assert_eq!(rows.remove(&7).as_deref(), Some("ready"));pub fn try_insert(self, key: K, value: V) -> Result<Option<V>, AccessError>
Sourcepub fn remove(self, key: &K) -> Option<V>
pub fn remove(self, key: &K) -> Option<V>
Missing-key removal is a no-op (no versions, work or wake).
pub fn try_remove(self, key: &K) -> Result<Option<V>, AccessError>
Source§impl<T> Cage<T>
impl<T> Cage<T>
Sourcepub fn field<U: 'static>(
self,
name: impl Into<Cow<'static, str>>,
get: impl for<'a> Fn(&'a T) -> &'a U + 'static,
get_mut: impl for<'a> Fn(&'a mut T) -> &'a mut U + 'static,
) -> Cage<U>
pub fn field<U: 'static>( self, name: impl Into<Cow<'static, str>>, get: impl for<'a> Fn(&'a T) -> &'a U + 'static, get_mut: impl for<'a> Fn(&'a mut T) -> &'a mut U + 'static, ) -> Cage<U>
Borrow and track a named inline field without cloning its parent model.
Accessors must be pure, select the same fixed subobject, and have no
reactive reads. Different sibling names must denote disjoint storage;
overlapping views use a common parent field then nested fields instead.
Heap elements/Deref projections and moving enum layouts are rejected.
Use item/entry for the supported Vec and keyed-map projections.
Aliases belong to the constructing Owner (or this Cage’s owner outside
construction); the last alias/child releases the canonical field path.
Field writes notify whole-model and ancestor readers, plus this field’s
aliases and descendants. Whole-model writes conservatively notify every
registered field. No value equality is implied by either operation.
let runtime = glory_core::Runtime::new();
let owner = runtime.owner();
let model = owner.cage((String::from("hello"), 0usize));
let title = model.field("title", |m| &m.0, |m| &mut m.0);
title.revise(|s| s.push('!'));
assert_eq!(model.with(|m| m.0.clone()), "hello!");pub fn try_field<U: 'static>( self, name: impl Into<Cow<'static, str>>, get: impl for<'a> Fn(&'a T) -> &'a U + 'static, get_mut: impl for<'a> Fn(&'a mut T) -> &'a mut U + 'static, ) -> Result<Cage<U>, AccessError>
Source§impl<T> Cage<T>
impl<T> Cage<T>
pub fn runtime(self) -> RuntimeId
pub fn try_with<R>(self, f: impl FnOnce(&T) -> R) -> Result<R, AccessError>
pub fn with<R>(self, f: impl FnOnce(&T) -> R) -> R
pub fn try_with_untracked<R>( self, f: impl FnOnce(&T) -> R, ) -> Result<R, AccessError>
pub fn with_untracked<R>(self, f: impl FnOnce(&T) -> R) -> R
pub fn try_get(self) -> Result<T, AccessError>where
T: Clone,
pub fn get(self) -> Twhere
T: Clone,
pub fn get_untracked(self) -> Twhere
T: Clone,
pub fn is_alive(self) -> bool
Source§impl<T> Cage<T>
impl<T> Cage<T>
Sourcepub fn try_version(self) -> Result<u64, AccessError>
pub fn try_version(self) -> Result<u64, AccessError>
Current source version, without subscribing or borrowing the value. Every successful write advances this stamp, even when equal data is restored. The identity is generation checked; a live collection path may still refer to a missing value. This does not flush pending work.