Struct Node

Source
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 Pinned. 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>

Source

pub fn builder(content: T) -> NodeBuilder<T>

Source

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().

Source

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().

Source

pub fn next_sibling(&self) -> Option<&Self>

Returns the Node immediately following this one in the parent’s children. Otherwise returns None if self has no parent, or if it is the last child of the parent.

Source

pub fn prev_sibling(&self) -> Option<&Self>

Returns the Node immediately preceeding this one in the parent’s children. Otherwise returns None if self has no parent, or if it is the first child of the parent.

Source

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().

Source

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().

Source

pub fn iter_bfs(&self) -> IterBFS<'_, T>

Iterate over all the Nodes of the subtree (including self) using Breadth-First Search.

Source

pub fn iter_dfs(&self) -> IterDFS<'_, T>

Iterate over all the Nodes of the subtree (including self) using Depth-First Search.

Source

pub fn is_same_as(&self, other: impl AsPtr<Raw = Self>) -> bool

Whether two Nodes are the same (that is, they reference the same object).

Source

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.

Source§

impl<T: Clone> Node<T>

Source

pub fn clone_deep(&self) -> Tree<T>

Copies the Node’s content and its children recursively. The resulting cloned Node will have no parent.

For a method that clones the Node but not its subtree, see Node::clone.

Source§

impl<T: Debug> Node<T>

Source

pub fn debug_tree(&self) -> DebugTree<'_, T>

Debug the entire subtree (self and its children).

Trait Implementations§

Source§

impl<T: Clone> Clone for Node<T>

Source§

fn clone(&self) -> Self

Copies the Node’s content, but not its children. The resulting cloned Node will have no parent or children.

Converting the returned Node to a Tree is recommended.

For a method that clones the Node and its subtree, see Node::clone_deep.

1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Node<T>

Source§

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

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

impl<T: Default> Default for Node<T>

Source§

fn default() -> Self

Creates a Node with the Default content. Converting the returned Node to a Tree is recommended.

Source§

impl<T: PartialEq> PartialEq for Node<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq> Eq for Node<T>

Auto Trait Implementations§

§

impl<T> Freeze for Node<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Node<T>
where T: RefUnwindSafe,

§

impl<T> !Send for Node<T>

§

impl<T> !Sync for Node<T>

§

impl<T> !Unpin for Node<T>

§

impl<T> UnwindSafe for Node<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.