pub struct TreeModelHandle<T> { /* private fields */ }Expand description
UI-local shared handle for a retained tree model.
Clones share one Rc<RefCell<_>> model and subscriber registry, so the handle
is single-threaded. Successful nonempty writes notify live weak subscribers
after releasing internal mutable borrows.
§Examples
use ailloli_ui_widgets::controls::{TreeItem, TreeModel, TreeModelHandle, TreeMutation};
let handle = TreeModelHandle::new(TreeModel::new());
handle.apply(TreeMutation::Insert { parent: None, index: 0, item: TreeItem::leaf(1, "file") }).unwrap();
assert_eq!(handle.revision(), 1);Implementations§
Source§impl<T> TreeModelHandle<T>
impl<T> TreeModelHandle<T>
Sourcepub fn new(model: TreeModel<T>) -> Self
pub fn new(model: TreeModel<T>) -> Self
Wraps an existing model in a new unshared subscriber registry.
§Examples
use ailloli_ui_widgets::controls::{TreeModel, TreeModelHandle};
let handle = TreeModelHandle::new(TreeModel::<u64>::new());
assert_eq!(handle.revision(), 0);Sourcepub fn read<R>(&self, read: impl FnOnce(&TreeModel<T>) -> R) -> R
pub fn read<R>(&self, read: impl FnOnce(&TreeModel<T>) -> R) -> R
Runs read under a shared borrow and returns its result.
§Panics
Panics on conflicting reentrant model access. Do not call handle mutation methods from inside the closure.
§Examples
use ailloli_ui_widgets::controls::{TreeModel, TreeModelHandle};
let handle = TreeModelHandle::new(TreeModel::<u8>::new());
assert_eq!(handle.read(|model| model.len()), 0);Sourcepub fn apply(
&self,
mutation: TreeMutation<T>,
) -> Result<TreeModelDelta<T>, TreeModelError<T>>
pub fn apply( &self, mutation: TreeMutation<T>, ) -> Result<TreeModelDelta<T>, TreeModelError<T>>
Atomically applies one mutation and notifies on nonempty success.
§Errors
Returns model validation or revision-overflow errors.
§Panics
Panics on conflicting reentrant model/subscriber borrows.
§Examples
use ailloli_ui_widgets::controls::{TreeItem, TreeModel, TreeModelHandle, TreeMutation};
let handle = TreeModelHandle::new(TreeModel::new());
handle.apply(TreeMutation::Insert { parent: None, index: 0, item: TreeItem::leaf(1, "file") }).unwrap();
assert_eq!(handle.read(|model| model.len()), 1);Sourcepub fn apply_batch(
&self,
mutations: impl IntoIterator<Item = TreeMutation<T>>,
) -> Result<TreeModelDelta<T>, TreeModelError<T>>
pub fn apply_batch( &self, mutations: impl IntoIterator<Item = TreeMutation<T>>, ) -> Result<TreeModelDelta<T>, TreeModelError<T>>
Atomically applies a batch and notifies once on nonempty success.
Empty batches do not notify or advance revision.
§Errors
Returns the first model validation error without committing candidate state.
§Panics
Panics on conflicting reentrant model/subscriber borrows.
§Examples
use ailloli_ui_widgets::controls::{TreeItem, TreeModel, TreeModelHandle, TreeMutation};
let handle = TreeModelHandle::new(TreeModel::new());
let delta = handle.apply_batch([TreeMutation::Insert { parent: None, index: 0, item: TreeItem::leaf(1, "file") }]).unwrap();
assert_eq!(delta.revision(), 1);Sourcepub fn apply_batch_if_revision(
&self,
expected_revision: u64,
mutations: impl IntoIterator<Item = TreeMutation<T>>,
) -> Result<TreeModelDelta<T>, TreeModelError<T>>
pub fn apply_batch_if_revision( &self, expected_revision: u64, mutations: impl IntoIterator<Item = TreeMutation<T>>, ) -> Result<TreeModelDelta<T>, TreeModelError<T>>
Applies and notifies only when expected_revision is still current.
§Errors
Returns TreeModelError::StaleRevision on mismatch or another model
validation error without committing candidate state.
§Panics
Panics on conflicting reentrant model/subscriber borrows.
§Examples
use ailloli_ui_widgets::controls::{TreeModel, TreeModelError, TreeModelHandle, TreeMutation};
let handle = TreeModelHandle::new(TreeModel::<u8>::new());
let error = handle.apply_batch_if_revision(2, Vec::<TreeMutation<u8>>::new()).unwrap_err();
assert_eq!(error, TreeModelError::StaleRevision { expected: 2, actual: 0 });Sourcepub fn subscribe(&self, callback: &Rc<dyn Fn(u64)>) -> TreeModelSubscription
pub fn subscribe(&self, callback: &Rc<dyn Fn(u64)>) -> TreeModelSubscription
Registers a weak revision listener.
The model never owns the target; dropping either the callback or returned guard removes the edge. The callback runs after model/subscriber mutable borrows are released and receives the committed revision.
§Panics
Panics on conflicting reentrant subscriber-registry access.
§Examples
use std::cell::Cell;
use std::rc::Rc;
use ailloli_ui_widgets::controls::{TreeItem, TreeModel, TreeModelHandle, TreeMutation};
let handle = TreeModelHandle::new(TreeModel::new());
let seen = Rc::new(Cell::new(0));
let sink = seen.clone();
let callback: Rc<dyn Fn(u64)> = Rc::new(move |revision| sink.set(revision));
let _guard = handle.subscribe(&callback);
handle.apply(TreeMutation::Insert { parent: None, index: 0, item: TreeItem::leaf(1, "file") }).unwrap();
assert_eq!(seen.get(), 1);Trait Implementations§
Source§impl<T: Clone> Clone for TreeModelHandle<T>
impl<T: Clone> Clone for TreeModelHandle<T>
Source§fn clone(&self) -> TreeModelHandle<T>
fn clone(&self) -> TreeModelHandle<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T> Debug for TreeModelHandle<T>
impl<T> Debug for TreeModelHandle<T>
Auto Trait Implementations§
impl<T> Freeze for TreeModelHandle<T>
impl<T> !RefUnwindSafe for TreeModelHandle<T>
impl<T> !Send for TreeModelHandle<T>
impl<T> !Sync for TreeModelHandle<T>
impl<T> Unpin for TreeModelHandle<T>
impl<T> !UnwindSafe for TreeModelHandle<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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<A> IntoClickAction<A> for Awhere
A: Clone + 'static,
impl<A> IntoClickAction<A> for Awhere
A: Clone + 'static,
§fn into_click_action(self) -> ClickAction<A>
fn into_click_action(self) -> ClickAction<A>
Implements the into_click_action helper used by this module.