Enum RemoveBehavior

Source
pub enum RemoveBehavior {
    DropChildren,
    LiftChildren,
    OrphanChildren,
}
Expand description

Describes the possible behaviors of the Tree::remove_node method.

Variants§

§

DropChildren

All children will be dropped recursively. In other words, the entire sub-tree of the Node being removed will be dropped from the tree. Those Nodes will no longer exist and cannot be accessed even if you have the NodeId the previously pointed to them.

This means even without using Clone you might end up with copies of invalid NodeIds. Use this behavior with caution.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::RemoveBehavior::*;

let mut tree: Tree<i32> = Tree::new();

let root_id = tree.insert(Node::new(0), AsRoot).unwrap();
let child_id = tree.insert(Node::new(1), UnderNode(&root_id)).unwrap();
let grandchild_id = tree.insert(Node::new(2), UnderNode(&child_id)).unwrap();

let child = tree.remove_node(child_id, DropChildren).ok().unwrap();

assert!(tree.get(&grandchild_id).is_err());
assert_eq!(tree.get(&root_id).unwrap().children().len(), 0);
assert_eq!(child.children().len(), 0);
assert_eq!(child.parent(), None);
§

LiftChildren

If the removed Node (let’s call it A) has a parent, A’s parent will become the parent of A’s children. This effectively just shifts them up one level in the Tree.

If A doesn’t have a parent, then this behaves exactly like RemoveBehavior::OrphanChildren.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::RemoveBehavior::*;

let mut tree: Tree<i32> = Tree::new();

let root_id = tree.insert(Node::new(0), AsRoot).unwrap();
let child_id = tree.insert(Node::new(1), UnderNode(&root_id)).unwrap();
let grandchild_id = tree.insert(Node::new(2), UnderNode(&child_id)).unwrap();

let child = tree.remove_node(child_id, LiftChildren).ok().unwrap();

assert!(tree.get(&grandchild_id).is_ok());
assert!(tree.get(&root_id).unwrap().children().contains(&grandchild_id));
assert_eq!(child.children().len(), 0);
assert_eq!(child.parent(), None);
§

OrphanChildren

All children will have their parent references cleared. This means nothing will point to them, but they will still exist in the tree. Those Nodes can still be accessed provided that you have the NodeId that points to them.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::RemoveBehavior::*;

let mut tree: Tree<i32> = Tree::new();

let root_id = tree.insert(Node::new(0), AsRoot).unwrap();
let child_id = tree.insert(Node::new(1), UnderNode(&root_id)).unwrap();
let grandchild_id = tree.insert(Node::new(2), UnderNode(&child_id)).unwrap();

let child = tree.remove_node(child_id, OrphanChildren).ok().unwrap();

assert!(tree.get(&grandchild_id).is_ok());
assert_eq!(tree.get(&root_id).unwrap().children().len(), 0);
assert_eq!(child.children().len(), 0);
assert_eq!(child.parent(), None);

Auto Trait Implementations§

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> 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, 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.