Struct TreeModelHandle

Source
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>
where T: Clone + Eq + Hash + Debug,

Source

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

pub fn revision(&self) -> u64

Returns the shared model’s current revision.

§Panics

Panics on reentrant access while the model is mutably borrowed.

§Examples
use ailloli_ui_widgets::controls::{TreeModel, TreeModelHandle};
assert_eq!(TreeModelHandle::new(TreeModel::<u8>::new()).revision(), 0);
Source

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

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

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

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

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>

Source§

fn clone(&self) -> TreeModelHandle<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for TreeModelHandle<T>
where T: Clone + Eq + Hash + Debug,

Source§

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

Formats revision and active/visible counts without dumping hierarchy.

Source§

impl<T> Default for TreeModelHandle<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

§

impl<A> IntoClickAction<A> for A
where A: Clone + 'static,

§

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> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> Props for T
where T: Clone + 'static,