[][src]Enum id_tree::MoveBehavior

pub enum MoveBehavior<'a> {
    ToRoot,
    ToParent(&'a NodeId),
}

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

Variants

ToRoot

Sets the Node in question as the new root Node, leaving all children in their place (in other words, they will travel with the Node being moved).

If there is already a root Node in place, it will be attached as the last child of the new root.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::MoveBehavior::*;

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

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

tree.move_node(&grandchild_id, ToRoot).unwrap();

assert_eq!(tree.root_node_id(), Some(&grandchild_id));
assert!(tree.get(&grandchild_id).unwrap().children().contains(&root_id));
assert!(!tree.get(&child_id).unwrap().children().contains(&grandchild_id));
ToParent(&'a NodeId)

Moves a Node inside the Tree to a new parent leaving all children in their place.

If the new parent (let's call it B) is a descendant of the Node being moved (A), then the direct child of A on the path from A to B will be shifted upwards to take the place of its parent (A). All other children of A will be left alone, meaning they will travel with it down the Tree.

Please note that during the "shift-up" part of the above scenario, the Node being shifted up will always be added as the last child of its new parent.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::MoveBehavior::*;

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

let root_id = tree.insert(Node::new(1), AsRoot).ok().unwrap();
let first_child_id = tree.insert(Node::new(2),  UnderNode(&root_id)).unwrap();
let second_child_id = tree.insert(Node::new(3), UnderNode(&root_id)).unwrap();
let grandchild_id = tree.insert(Node::new(4), UnderNode(&first_child_id)).unwrap();

tree.move_node(&grandchild_id, ToParent(&second_child_id)).unwrap();

assert!(!tree.get(&first_child_id).unwrap().children().contains(&grandchild_id));
assert!(tree.get(&second_child_id).unwrap().children().contains(&grandchild_id));

Auto Trait Implementations

impl<'a> Send for MoveBehavior<'a>

impl<'a> Sync for MoveBehavior<'a>

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = !

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]