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§
Sourcefn rename_node(&mut self, id: &Self::Id, new_name: String) -> bool
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 renamenew_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");
}Sourcefn remove_node(&mut self, id: &Self::Id) -> Option<Self>
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());
}Sourcefn insert_node(
&mut self,
target_id: &Self::Id,
node: Self,
position: DropPosition,
) -> bool
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 nodenode- The node to insertposition- 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");
}Sourcefn find_node_mut(&mut self, id: &Self::Id) -> Option<&mut Self>
fn find_node_mut(&mut self, id: &Self::Id) -> Option<&mut Self>
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.