TreeOperations

Trait TreeOperations 

Source
pub trait TreeOperations:
    OutlinerNode
    + Sized
    + Clone {
    // Provided methods
    fn rename_node(&mut self, id: &Self::Id, new_name: String) -> bool { ... }
    fn remove_node(&mut self, id: &Self::Id) -> Option<Self> { ... }
    fn insert_node(
        &mut self,
        target_id: &Self::Id,
        node: Self,
        position: DropPosition,
    ) -> bool { ... }
    fn find_node(&self, id: &Self::Id) -> Option<&Self> { ... }
    fn find_node_mut(&mut self, id: &Self::Id) -> Option<&mut Self> { ... }
}
Expand description

Trait providing tree manipulation operations for outliner nodes.

This trait offers default implementations for common tree operations:

  • Renaming: Find and update a node’s name by ID
  • Removing: Extract a node from the tree by ID
  • Inserting: Place a node at a specific position relative to a target

All methods use recursive traversal to locate nodes within the tree hierarchy.

§Default Implementations

Simply implement this trait with an empty body to get all operations:

impl TreeOperations for MyNode {}

§Custom Implementations

You can override any method to provide custom behavior:

impl TreeOperations for MyNode {
    fn rename_node(&mut self, id: &Self::Id, new_name: String) -> bool {
        // Custom rename logic
        // ...
    }
    // Use default implementations for other methods
}

Provided Methods§

Source

fn rename_node(&mut self, id: &Self::Id, new_name: String) -> bool

Finds a node by ID and updates its name.

This method recursively searches the tree starting from this node, looking for a node with the specified ID. When found, it updates the node’s name.

§Arguments
  • id - The ID of the node to rename
  • new_name - The new name for the node
§Returns

true if the node was found and renamed, false otherwise.

§Examples
if root.rename_node(&node_id, "New Name".to_string()) {
    println!("Node renamed successfully");
}
Source

fn remove_node(&mut self, id: &Self::Id) -> Option<Self>

Removes a node from the tree by ID and returns it.

This method recursively searches the tree starting from this node’s children, looking for a node with the specified ID. When found, it removes the node from its parent’s children list and returns it.

§Arguments
  • id - The ID of the node to remove
§Returns

Some(node) if the node was found and removed, None otherwise.

§Examples
if let Some(removed_node) = root.remove_node(&node_id) {
    println!("Removed node: {}", removed_node.name());
}
Source

fn insert_node( &mut self, target_id: &Self::Id, node: Self, position: DropPosition, ) -> bool

Inserts a node at a specific position relative to a target node.

This method recursively searches for the target node and inserts the new node according to the specified position:

  • Before: Insert before the target node (as a sibling)
  • After: Insert after the target node (as a sibling)
  • Inside: Insert as a child of the target node (only for collections)
§Arguments
  • target_id - The ID of the target node
  • node - The node to insert
  • position - Where to insert relative to the target
§Returns

true if the node was successfully inserted, false otherwise.

§Examples
let new_node = MyNode::new(42, "New Node");
if root.insert_node(&target_id, new_node, DropPosition::Inside) {
    println!("Node inserted successfully");
}
Source

fn find_node(&self, id: &Self::Id) -> Option<&Self>

Finds a node by ID in the tree.

This is a helper method that recursively searches for a node with the given ID.

§Arguments
  • id - The ID of the node to find
§Returns

A reference to the node if found, None otherwise.

Source

fn find_node_mut(&mut self, id: &Self::Id) -> Option<&mut Self>

Finds a node by ID in the tree (mutable version).

This is a helper method that recursively searches for a node with the given ID and returns a mutable reference.

§Arguments
  • id - The ID of the node to find
§Returns

A mutable reference to the node if found, None otherwise.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§