Skip to main content

Cage

Struct Cage 

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

Source

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.

Source

pub fn try_item(self, index: usize) -> Result<Cage<T>, AccessError>

Source§

impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>

Source

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

pub fn try_entry(self, key: K) -> Result<Cage<V>, AccessError>

Source§

impl<K: Eq + Hash + 'static, V: 'static, S: BuildHasher + 'static> Cage<HashMap<K, V, S>>

Source

pub fn entry(self, key: K) -> Cage<V>

HashMap counterpart of BTreeMap’s entry; preserves the map’s hasher. Key hashing and comparison must be pure; no value Clone bound is needed.

Source

pub fn try_entry(self, key: K) -> Result<Cage<V>, AccessError>

Source§

impl<T: 'static> Cage<Vec<T>>

Source

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

pub fn try_push(self, value: T) -> Result<(), AccessError>

Source

pub fn pop(self) -> Option<T>

Source

pub fn try_pop(self) -> Result<Option<T>, AccessError>

Source

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.

Source

pub fn try_insert(self, index: usize, value: T) -> Result<(), AccessError>

Source

pub fn remove(self, index: usize) -> T

Source

pub fn try_remove(self, index: usize) -> Result<T, AccessError>

Source

pub fn truncate(self, len: usize)

Source

pub fn try_truncate(self, len: usize) -> Result<(), AccessError>

Source

pub fn clear(self)

Source

pub fn try_clear(self) -> Result<(), AccessError>

Source§

impl<K: Ord + Hash + 'static, V: 'static> Cage<BTreeMap<K, V>>

Source

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.

Source

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

pub fn try_insert(self, key: K, value: V) -> Result<Option<V>, AccessError>

Source

pub fn remove(self, key: &K) -> Option<V>

Missing-key removal is a no-op (no versions, work or wake).

Source

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

Source

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.

Source

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

pub fn try_insert(self, key: K, value: V) -> Result<Option<V>, AccessError>

Source

pub fn remove(self, key: &K) -> Option<V>

Missing-key removal is a no-op (no versions, work or wake).

Source

pub fn try_remove(self, key: &K) -> Result<Option<V>, AccessError>

Source§

impl<T> Cage<T>

Source

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

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>

Source

pub fn runtime(self) -> RuntimeId

Source

pub fn try_with<R>(self, f: impl FnOnce(&T) -> R) -> Result<R, AccessError>

Source

pub fn with<R>(self, f: impl FnOnce(&T) -> R) -> R

Source

pub fn try_with_untracked<R>( self, f: impl FnOnce(&T) -> R, ) -> Result<R, AccessError>

Source

pub fn with_untracked<R>(self, f: impl FnOnce(&T) -> R) -> R

Source

pub fn try_get(self) -> Result<T, AccessError>
where T: Clone,

Source

pub fn get(self) -> T
where T: Clone,

Source

pub fn get_untracked(self) -> T
where T: Clone,

Source

pub fn is_alive(self) -> bool

Source§

impl<T> Cage<T>

Source

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.

Source

pub fn try_revise<R>( self, f: impl FnOnce(&mut T) -> R, ) -> Result<R, AccessError>

Source

pub fn revise<R>(self, f: impl FnOnce(&mut T) -> R) -> R

Source

pub fn map<U: 'static>(self, mapper: impl Fn(&T) -> U + 'static) -> Bond<U>

Trait Implementations§

Source§

impl<T> Clone for Cage<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for Cage<T>

Source§

impl<T> Debug for Cage<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> From<Cage<T>> for Vary<T>

Source§

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

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> !Send for Cage<T>

§

impl<T> !Sync for Cage<T>

§

impl<T> Forget for Cage<T>

§

impl<T> Freeze for Cage<T>
where PhantomData<Rc<T>>: Freeze,

§

impl<T> Move for Cage<T>

§

impl<T> RefUnwindSafe for Cage<T>
where Rc<T>: RefUnwindSafe,

§

impl<T> Unpin for Cage<T>
where Rc<T>: Unpin,

§

impl<T> UnsafeUnpin for Cage<T>

§

impl<T> UnwindSafe for Cage<T>
where Rc<T>: UnwindSafe,

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<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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.