pub struct Node<T> {
pub content: T,
/* private fields */
}
Expand description
A Node
has 1 parent
and multiple children
.
It also stores content
of type T
.
A Node is heap-allocated
and pinned
to allow storing a reference to the parent (Node is a self referential struct)
without the data of said parent being moved.
The pointer to the parent must be valid for the lifetime of the Node that holds the pointer.
Therefore, in theory, a stack-allocated unpinned Node should not exist, but that is ok as long as the Node has no children.
The current implementation of the methods allows asserting that such Node has no children
because adding children
(i.e. using a mutable Node) requires it to be Pin
ned.
A user can still use std::pin::pin!
on a stack-allocated Node and add children to it,
but the Node can’t be moved, and its children are dropped along with it,
so the pointer it’s children hold remains valid for their lifetimes.
This allows the Node struct to implement traits that require returning a stack-allocated Node (e.g. Default
and Clone
).
However, it is recommended to convert the returned Node
into a Tree
using Tree::from()
or Node::into()
as an “ez mode”
for getting rid of compiler errors that are caused by trying to use &mut Node
or trying to move it.
Fields§
§content: T
Implementations§
Source§impl<T> Node<T>
impl<T> Node<T>
pub fn builder(content: T) -> NodeBuilder<T>
Sourcepub fn parent(&self) -> Option<&Self>
pub fn parent(&self) -> Option<&Self>
Get an immutable reference to the parent
Node
of self
.
To get a mutable reference,
call crate::Tree::borrow_descendant()
from the owner Tree
with self.parent().ptr()
.
Sourcepub fn children(&self) -> Box<[&Self]>
pub fn children(&self) -> Box<[&Self]>
Holds references to each child.
/// To get a mutable reference to one of the children,
call crate::Tree::borrow_descendant()
from the owner Tree
with self.parent().ptr()
.
Sourcepub fn next_sibling(&self) -> Option<&Self>
pub fn next_sibling(&self) -> Option<&Self>
Sourcepub fn prev_sibling(&self) -> Option<&Self>
pub fn prev_sibling(&self) -> Option<&Self>
Sourcepub fn append_child(self: Pin<&mut Self>, child: Tree<T>)
pub fn append_child(self: Pin<&mut Self>, child: Tree<T>)
Pushes the child to the end of self’s children.
Also see Self::insert_child()
.
Sourcepub fn insert_child(self: Pin<&mut Self>, child: Tree<T>, index: usize)
pub fn insert_child(self: Pin<&mut Self>, child: Tree<T>, index: usize)
Inserts the child to self’s children at some index.
Also see Self::append_child()
.
Sourcepub fn iter_bfs(&self) -> IterBFS<'_, T> ⓘ
pub fn iter_bfs(&self) -> IterBFS<'_, T> ⓘ
Iterate over all the Node
s of the subtree (including self
) using Breadth-First Search.
Sourcepub fn iter_dfs(&self) -> IterDFS<'_, T> ⓘ
pub fn iter_dfs(&self) -> IterDFS<'_, T> ⓘ
Iterate over all the Node
s of the subtree (including self
) using Depth-First Search.
Sourcepub fn is_same_as(&self, other: impl AsPtr<Raw = Self>) -> bool
pub fn is_same_as(&self, other: impl AsPtr<Raw = Self>) -> bool
Whether two Node
s are the same (that is, they reference the same object).
Sourcepub fn ptr(&self) -> NonNull<Self>
pub fn ptr(&self) -> NonNull<Self>
Get a NonNull
pointer for self, which should only be treated as a *const Self
.
Useful for Tree::detach_descendant
and Tree::borrow_descendant
.