pub struct Tree<T> { /* private fields */ }
Expand description
Implementations§
Source§impl<T> Tree<T>
impl<T> Tree<T>
pub fn builder(content: T) -> NodeBuilder<T>
pub fn root(&self) -> &Node<T>
pub fn root_mut(&mut self) -> Pin<&mut Node<T>>
Sourcepub fn detach_descendant(
&mut self,
descendant: NonNull<Node<T>>,
) -> Option<Self>
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 Pin
ned.
§Example
let target = tree.root().children()[1].ptr();
let detached = tree.detach_descendant(target).unwrap();
assert!(detached.root().is_same_as(target));
Sourcepub fn borrow_descendant(
&mut self,
descendant: NonNull<Node<T>>,
) -> Option<Pin<&mut Node<T>>>
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 Pin
ned.
§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
.
Trait Implementations§
Source§impl<T: Clone> Clone for Tree<T>
impl<T: Clone> Clone for Tree<T>
Source§fn clone(&self) -> Self
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)
fn clone_from(&mut self, source: &Self)
source
. Read more