Struct Tree

Source
pub struct Tree<T> { /* private fields */ }
Expand description

A Tree of Nodes. The root of the Tree has no parent.

§Ownership

When a Node method returns this type, it means it is passing ownership of the Nodes.

When a Node method asks for this type as argument, it means it is taking ownership of the Nodes.

Implementations§

Source§

impl<T> Tree<T>

Source

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

Source

pub fn root(&self) -> &Node<T>

Source

pub fn root_mut(&mut self) -> Pin<&mut Node<T>>

Source

pub fn detach_descendant( &mut self, descendant: NonNull<Node<T>>, ) -> Option<Self>

Removes the descendant of the root Node from the Tree, and returns the detached Node with ownership (aka a Tree).

Returns None if it is not a descendant of the root, or root is_same_as descendant.

This function can only be called from the root Node.

descendant must be a NonNull pointer (obtained from Node::ptr) because, if it was a reference, the borrow checker will consider the entire Tree to be immutably borrowed (including self). The descendant pointer passed to this function will remain valid because it is Pinned.

§Example
let target = tree.root().children()[1].ptr();
let detached = tree.detach_descendant(target).unwrap();
assert!(detached.root().is_same_as(target));
Source

pub fn borrow_descendant( &mut self, descendant: NonNull<Node<T>>, ) -> Option<Pin<&mut Node<T>>>

Mutably borrows a descendant of the Tree’s root Node as mutable. See Mutable Iterators section for why obtaining a &mut Node was implemented this way.

Returns None if it is not a descendant of the root, or root is_same_as descendant.

This function can be used to mutably borrow a Node obtained by a Node iterator.

descendant must be a NonNull pointer (obtained from Node::ptr) because, if it was a reference, the borrow checker will consider the entire Tree to be immutably borrowed (including self). The descendant pointer passed to this function will remain valid because it is Pinned.

§Example
let target = tree.iter_bfs().find(|n| n.content == 'c').unwrap().ptr();
let borrowed = tree.borrow_descendant(target).unwrap();
assert!(borrowed.is_same_as(target));

It should be enough to assert that the whole Tree is mut, so by extension the descendant is also mut.

Source

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

Iterate over all the Nodes of the Tree using Breadth-First Search.

Source

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

Iterate over all the Nodes of the Tree using Depth-First Search.

Trait Implementations§

Source§

impl<T: Clone> Clone for Tree<T>

Source§

fn clone(&self) -> Self

Clones the entire Tree by calling Node::clone_deep() on the root.

1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Tree<T>

Source§

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

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

impl<T: Default> Default for Tree<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> From<NodeBuilder<T>> for Tree<T>

Source§

fn from(builder: NodeBuilder<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> IntoIterator for &'a Tree<T>

Source§

type Item = &'a Node<T>

The type of the elements being iterated over.
Source§

type IntoIter = IterBFS<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: PartialEq> PartialEq for Tree<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 Tree<T>

Auto Trait Implementations§

§

impl<T> Freeze for Tree<T>

§

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

§

impl<T> !Send for Tree<T>

§

impl<T> !Sync for Tree<T>

§

impl<T> Unpin for Tree<T>

§

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