Trait NodeApi

Source
pub trait NodeApi {
    // Required methods
    fn id(self: &Arc<Self>) -> &NodeId;
    fn create_child(self: &Arc<Self>) -> Result<Arc<Node>, Box<dyn Error>>;
    fn create_child_at(
        self: &Arc<Self>,
        index: usize,
    ) -> Result<Arc<Node>, Box<dyn Error>>;
    fn create_child_with_id(
        self: &Arc<Self>,
        id: impl Into<NodeId>,
    ) -> Result<Arc<Node>, Box<dyn Error>>;
    fn create_child_with_id_at(
        self: &Arc<Self>,
        id: impl Into<NodeId>,
        index: usize,
    ) -> Result<Arc<Node>, Box<dyn Error>>;
    fn move_to(
        self: &Arc<Self>,
        parent: &Node,
        index: Option<usize>,
    ) -> Result<(), Box<dyn Error>>;
    fn move_before(
        self: &Arc<Self>,
        other: &Arc<Node>,
    ) -> Result<(), Box<dyn Error>>;
    fn move_after(
        self: &Arc<Self>,
        other: &Arc<Node>,
    ) -> Result<(), Box<dyn Error>>;
    fn children(self: &Arc<Self>) -> Vec<Arc<Node>>;
    fn parent(self: &Arc<Self>) -> Option<Arc<Node>>;
    fn siblings(self: &Arc<Self>) -> Vec<Arc<Node>>;
    fn depth(self: &Arc<Self>) -> usize;
}
Expand description

A trait for objects that can behave like a node in a tree; this is implemented for Node and Tree. When these methods are used on a Tree, they behave as if they were called on the root node.

Required Methods§

Source

fn id(self: &Arc<Self>) -> &NodeId

Returns the ID of the node.

Source

fn create_child(self: &Arc<Self>) -> Result<Arc<Node>, Box<dyn Error>>

Creates a new child node with a generated ID.

Source

fn create_child_at( self: &Arc<Self>, index: usize, ) -> Result<Arc<Node>, Box<dyn Error>>

Creates a new child node with a generated ID at the given index in the parent’s children.

Source

fn create_child_with_id( self: &Arc<Self>, id: impl Into<NodeId>, ) -> Result<Arc<Node>, Box<dyn Error>>

Creates a new child node with the given ID at the end of the parent’s children.

Source

fn create_child_with_id_at( self: &Arc<Self>, id: impl Into<NodeId>, index: usize, ) -> Result<Arc<Node>, Box<dyn Error>>

Creates a new child node with the given ID at the given index in the parent’s children.

Source

fn move_to( self: &Arc<Self>, parent: &Node, index: Option<usize>, ) -> Result<(), Box<dyn Error>>

Moves the node to the given parent, placing it in that parent’s children at the given index.

Given:

<ROOT>
├──A
│  ├──C
│  ├──D
│  └──E
└──B

If we call B.move_to(&A, Some(1)), we get:

<ROOT>
└──A
   ├──C
   ├──B
   ├──D
   └──E

Passing None as the index moves the node to the end of the parent’s children.

Source

fn move_before( self: &Arc<Self>, other: &Arc<Node>, ) -> Result<(), Box<dyn Error>>

Moves the node before the given node.

Given:

<ROOT>
├──A
│  ├──C
│  ├──D
│  └──E
└──B

If we call B.move_before(&E), we get:

<ROOT>
└──A
   ├──C
   ├──D
   ├──B
   └──E
Source

fn move_after(self: &Arc<Self>, other: &Arc<Node>) -> Result<(), Box<dyn Error>>

Moves the node after the given node.

Given:

<ROOT>
├──A
│  ├──C
│  ├──D
│  └──E
└──B

If we call B.move_after(&E), we get:

<ROOT>
└──A
   ├──C
   ├──D
   ├──E
   └──B
Source

fn children(self: &Arc<Self>) -> Vec<Arc<Node>>

Returns the children of the node.

Source

fn parent(self: &Arc<Self>) -> Option<Arc<Node>>

Returns the parent of the node.

Source

fn siblings(self: &Arc<Self>) -> Vec<Arc<Node>>

Returns the siblings of the node.

Source

fn depth(self: &Arc<Self>) -> usize

Returns the depth of the node. The root node has a depth of 0; all other nodes have a depth of 1 plus the depth of their parent.

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§

Source§

impl NodeApi for Node

Source§

impl NodeApi for Tree

Tree implements NodeApi, forwarding the calls to the root node of the tree