[][src]Enum id_tree::SwapBehavior

pub enum SwapBehavior {
    TakeChildren,
    LeaveChildren,
    ChildrenOnly,
}

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

Variants

TakeChildren

Take the children of the Nodes being swapped with them. In other words, this swaps the Nodes in question along with their entire sub-tree. This does not affect the relationship between the Nodes being swapped and their children.

If one Node is a descendant of the other getting swapped, the former upper Node is attached as the last child of the former lower Node after the swap. (The lower will take the uppers original position as usual.) The subtree of the former upper node is not touched except that the lower Node is moved including all its children.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::SwapBehavior::*;

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

let root_id = tree.insert(Node::new(1), AsRoot).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(&second_child_id)).unwrap();

tree.swap_nodes(&first_child_id, &grandchild_id, TakeChildren).unwrap();

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

Leave the children of the Nodes being swapped where they are. In other words, this only swaps the Nodes themselves. This does affect the relationship between the Nodes being swapped and their children.

Please Note: Because this behavior alters the relationship between the Nodes being swapped and their children, any calls to children() that have been cloned will no longer point to the children of the Node that you might think they do.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::SwapBehavior::*;

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

let root_id = tree.insert(Node::new(1), AsRoot).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(&second_child_id)).unwrap();

tree.swap_nodes(&first_child_id, &second_child_id, LeaveChildren).unwrap();

assert!(tree.get(&first_child_id).unwrap().children().contains(&grandchild_id));
assert_eq!(tree.get(&second_child_id).unwrap().children().len(), 0);
ChildrenOnly

Swap the children of the Nodes in question only. This does not swap the Nodes that are specified when calling this method. This does affect the relationship between the Nodes that are specified and their children.

If one Node is a descendant of the other getting swapped, the child swapping step will take place and then the lower Node in the swap will be added as the last child of the upper Node in the swap.

Please Note: Because this behavior alters the relationship between the Nodes being swapped and their children, any calls to children() that have been cloned will no longer point to the children of the Node that you think they do.

use id_tree::*;
use id_tree::InsertBehavior::*;
use id_tree::SwapBehavior::*;

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

let root_id = tree.insert(Node::new(1), AsRoot).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(&second_child_id)).unwrap();
let grandchild_id_2 = tree.insert(Node::new(5), UnderNode(&first_child_id)).unwrap();

tree.swap_nodes(&first_child_id, &second_child_id, ChildrenOnly).unwrap();

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

Auto Trait Implementations

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 = Infallible

The type returned in the event of a conversion error.

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

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

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

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

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

The type returned in the event of a conversion error.