Skip to main content

TreeModel

Struct TreeModel 

Source
pub struct TreeModel<T: 'static> { /* private fields */ }
Expand description

A concrete reactive tree that stores a hierarchy of T items in a flat arena.

TreeModel<T> is Clone — cloning produces a second handle to the same underlying data. All handles see the same hierarchy and receive the same TreeChange notifications from observe_changes. Nodes are identified by opaque NodeId handles that are stable and non-reusable across mutations (versioned SlotMap keys).

Implementations§

Source§

impl<T: 'static> TreeModel<T>

Source

pub fn new() -> Self

Create an empty tree model with no roots and no observers.

Source

pub fn root_count(&self) -> usize

Number of root-level nodes.

Source

pub fn root(&self, index: usize) -> NodeId

Get the NodeId of a root-level node by index.

§Panics

Panics if index >= root_count().

Source

pub fn child_count(&self, parent: NodeId) -> usize

Number of children of the given node.

Source

pub fn child(&self, parent: NodeId, index: usize) -> NodeId

Get the NodeId of a child by parent and index.

§Panics

Panics if the parent or index is invalid.

Source

pub fn parent(&self, node: NodeId) -> Option<NodeId>

Get the parent of a node, or None if it is a root.

Source

pub fn depth(&self, node: NodeId) -> usize

Compute the depth of a node (0 for roots).

Source

pub fn has_children(&self, node: NodeId) -> bool

Whether the given node has any children.

Source

pub fn children(&self, node: NodeId) -> Vec<NodeId>

Get the children of a node as a vector of NodeId.

Source

pub fn with_item<R>(&self, node: NodeId, f: impl FnOnce(&T) -> R) -> Option<R>

Access a node’s data via a callback. Returns None if the node doesn’t exist.

Source

pub fn find_by(&self, predicate: impl Fn(&T) -> bool) -> Option<NodeId>

Find the first node matching a predicate (depth-first from roots).

Source

pub fn insert_root(&self, index: usize, item: T) -> NodeId

Insert a new root-level node at the given index.

§Panics

Panics if index > root_count().

Source

pub fn insert_child(&self, parent: NodeId, index: usize, item: T) -> NodeId

Insert a new child node under the given parent at the given index.

§Panics

Panics if the parent is invalid or index > child_count(parent).

Source

pub fn remove(&self, node: NodeId)

Remove a node and its entire subtree.

§Panics

Panics if the node is invalid.

Source

pub fn move_node(&self, node: NodeId, new_parent: NodeId, new_index: usize)

Move a node (and its subtree) to a new parent at the given index.

§Panics

Panics if any of the nodes are invalid, or if the target is a descendant of the source (would create a cycle).

Source

pub fn move_to_root(&self, node: NodeId, new_index: usize)

Move a node to the root level at the given index.

Source

pub fn update(&self, node: NodeId, item: T)

Update a node’s data in place.

§Panics

Panics if the node is invalid.

Source

pub fn observe_changes( &self, f: impl Fn(&TreeChange) + 'static, ) -> ObserverHandle

Register an observer for tree change notifications. Returns an ObserverHandle — dropping it removes the callback.

Source§

impl<T: Debug + 'static> TreeModel<T>

Source

pub fn debug_named(self, _name: impl Into<String>) -> Self

Register this tree with the debug inspector under name. In release builds (!cfg(debug_assertions)) this is a no-op pass-through so call sites stay free of #[cfg] lines.

Idempotent on repeated calls — the latest registration wins. The registration drops automatically when the last TreeModel handle is freed (the adapter the registry holds is Weak).

Trait Implementations§

Source§

impl<T: 'static> Clone for TreeModel<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: Debug + 'static> Debug for TreeModel<T>

Source§

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

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

impl<T: 'static> Default for TreeModel<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for TreeModel<T>

§

impl<T> !Send for TreeModel<T>

§

impl<T> !Sync for TreeModel<T>

§

impl<T> !UnwindSafe for TreeModel<T>

§

impl<T> Freeze for TreeModel<T>

§

impl<T> Unpin for TreeModel<T>

§

impl<T> UnsafeUnpin for TreeModel<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<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.