Struct Token

Source
pub struct Token { /* private fields */ }
Expand description

A Token is a handle to a node in the arena.

Implementations§

Source§

impl Token

Source

pub fn is_leaf<T>(self, arena: &Arena<T>) -> bool

Checks whether a given node is actually a leaf.

§Panics:

Panics if the token does not correspond to a node in the arena.

Source

pub fn append<T>(self, arena: &mut Arena<T>, data: T) -> Token

Creates a new node with the given data and append to the given node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let next_node_token = root_token.append(&mut arena, "Germanic");
next_node_token.append(&mut arena, "Romance");
let mut subtree = root_token.subtree(&arena, TraversalOrder::Pre);

assert_eq!(subtree.next().unwrap().data, "Indo-European");
assert_eq!(subtree.next().unwrap().data, "Germanic");
assert_eq!(subtree.next().unwrap().data, "Romance");
Source

pub fn insert_before<T>(self, arena: &mut Arena<T>, data: T) -> Token

Creates a new node with the given data and sets as the previous sibling of the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let child2 = root_token.append(&mut arena, "Germanic");
let child4 = root_token.append(&mut arena, "Slavic");
child2.append(&mut arena, "English");
// insert in between children 2 and 4
let child3 = child4.insert_before(&mut arena, "Romance");
// insert before child 2
let child1 = child2.insert_before(&mut arena, "Celtic");

let subtree: Vec<_> = root_token.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data)
    .collect();
assert_eq!(&["Indo-European", "Celtic", "Germanic", "English", "Romance", "Slavic"],
           &subtree[..]);
Source

pub fn insert_node_after<T>( self, arena: &mut Arena<T>, other: Token, ) -> Result<(), Error>

Set a node in the arena as the next sibling of the given node. Returns error if the “other node” is not a root node of a tree (as in it already has a parent and/or siblings).

Note: for performance reasons, this operation does not check whether the “self” node is in fact a descendant of the other tree. A cyclic graph may result.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

// root node that we will attach subtrees to
let root_data = "Indo-European";
let (mut arena, root) = Arena::with_data(root_data);

let germanic = root.append(&mut arena, "Germanic");
let scots = germanic.append(&mut arena, "German");
let english = germanic.append(&mut arena, "English");

let romance = arena.new_node("Romance");
let french = romance.append(&mut arena, "French");
let spanish = romance.append(&mut arena, "Spanish");

germanic.insert_node_after(&mut arena, romance);

let mut iter = root.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("Indo-European"));
assert_eq!(iter.next(), Some("Germanic"));
assert_eq!(iter.next(), Some("German"));
assert_eq!(iter.next(), Some("English"));
assert_eq!(iter.next(), Some("Romance"));
assert_eq!(iter.next(), Some("French"));
assert_eq!(iter.next(), Some("Spanish"));
assert!(iter.next().is_none())
Source

pub fn insert_node_before<T>( self, arena: &mut Arena<T>, other: Token, ) -> Result<(), Error>

Set a node in the arena as the previous sibling of the given node. Returns error if the “other node” is not a root node of a tree (as in it already has a parent and/or siblings).

Note: for performance reasons, this operation does not check whether the “self” node is in fact a descendant of the other tree. A cyclic graph may result.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

// root node that we will attach subtrees to
let root_data = "Indo-European";
let (mut arena, root) = Arena::with_data(root_data);

let germanic = root.append(&mut arena, "Germanic");
let scots = germanic.append(&mut arena, "German");
let english = germanic.append(&mut arena, "English");

let romance = arena.new_node("Romance");
let french = romance.append(&mut arena, "French");
let spanish = romance.append(&mut arena, "Spanish");

germanic.insert_node_before(&mut arena, romance);

let mut iter = root.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("Indo-European"));
assert_eq!(iter.next(), Some("Romance"));
assert_eq!(iter.next(), Some("French"));
assert_eq!(iter.next(), Some("Spanish"));
assert_eq!(iter.next(), Some("Germanic"));
assert_eq!(iter.next(), Some("German"));
assert_eq!(iter.next(), Some("English"));
assert!(iter.next().is_none())
Source

pub fn insert_after<T>(self, arena: &mut Arena<T>, data: T) -> Token

Creates a new node with the given data and sets as the next sibling of the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let child1 = root_token.append(&mut arena, "Romance");
let child3 = root_token.append(&mut arena, "Germanic");
child1.append(&mut arena, "French");
// insert betwern children 1 and 4
let child2 = child1.insert_after(&mut arena, "Celtic");
// insert after child 3
child3.insert_after(&mut arena, "Slavic");

let subtree: Vec<_> = root_token.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data)
    .collect();
assert_eq!(&["Indo-European", "Romance", "French", "Celtic", "Germanic", "Slavic"],
           &subtree[..]);
Source

pub fn append_node<T>( self, arena: &mut Arena<T>, other: Self, ) -> Result<(), Error>

Attaches a different tree in the arena to a node. Returns error if the “root node” of the other tree is not really a root node (as in it already has a parent and/or siblings). To attach a tree from a different arena, use copy_and_append_subtree instead.

Note: for performance reasons, this operation does not check whether the “self” node is in fact a descendant of the other tree. A cyclic graph may result.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

// root node that we will attach subtrees to
let root_data = "Indo-European";
let (mut arena, root) = Arena::with_data(root_data);

// the Germanic branch
let germanic = arena.new_node("Germanic");
let west = germanic.append(&mut arena, "West");
let scots = west.append(&mut arena, "Scots");
let english = west.append(&mut arena, "English");

// the Romance branch
let romance = arena.new_node("Romance");
let french = romance.append(&mut arena, "French");
let italian = romance.append(&mut arena, "Italian");

// attach subtrees
root.append_node(&mut arena, romance).unwrap();
root.append_node(&mut arena, germanic).unwrap();

let mut iter = root.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("Indo-European"));
assert_eq!(iter.next(), Some("Romance"));
assert_eq!(iter.next(), Some("French"));
assert_eq!(iter.next(), Some("Italian"));
assert_eq!(iter.next(), Some("Germanic"));
assert_eq!(iter.next(), Some("West"));
assert_eq!(iter.next(), Some("Scots"));
assert_eq!(iter.next(), Some("English"));
assert!(iter.next().is_none())
Source

pub fn detach<T>(self, arena: &mut Arena<T>)

Detaches the given node and its descendants into its own tree while keeping it in the same arena. To detach and allocate the subtree into its own arena, use split_at instead.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

// root node that we will attach subtrees to
let root_data = "Indo-European";
let (mut arena, root) = Arena::with_data(root_data);

// the Germanic branch
let germanic = root.append(&mut arena, "Germanic");
let west = germanic.append(&mut arena, "West");
let scots = west.append(&mut arena, "Scots");
let english = west.append(&mut arena, "English");

// detach the west branch from the main tree
west.detach(&mut arena);

// the west branch is gone from the original tree
let mut iter = root.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("Indo-European"));
assert_eq!(iter.next(), Some("Germanic"));
assert!(iter.next().is_none());

// it exists on its own
let mut iter = west.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("West"));
assert_eq!(iter.next(), Some("Scots"));
assert_eq!(iter.next(), Some("English"));
assert!(iter.next().is_none());
Source

pub fn replace_node<T>( self, arena: &mut Arena<T>, other: Token, ) -> Result<(), Error>

Replace the subtree of self with the subtree of other. Does not remove self or its descendants but simply makes it a standalone tree within the arena.

Note: for performance reasons, this operation does not check whether the “other” node is in fact a descendant of the parent tree of self. A cyclic graph may result.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

// root node that we will attach subtrees to
let root_data = "Indo-European";
let (mut arena, root) = Arena::with_data(root_data);

// the Germanic branch
let germanic = root.append(&mut arena, "Germanic");
let west = germanic.append(&mut arena, "West");
let scots = west.append(&mut arena, "Scots");
let english = west.append(&mut arena, "English");

// the slavic branch
let slavic = root.append(&mut arena, "Slavic");
let polish = slavic.append(&mut arena, "Polish");
let russian = slavic.append(&mut arena, "Russian");

// the Romance branch
let romance = arena.new_node("Romance");
let french = romance.append(&mut arena, "French");
let italian = romance.append(&mut arena, "Italian");

// replace_node germanic with romance
germanic.replace_node(&mut arena, romance).unwrap();

let mut iter = root.subtree(&arena, TraversalOrder::Pre)
    .map(|x| x.data);
assert_eq!(iter.next(), Some("Indo-European"));
assert_eq!(iter.next(), Some("Romance"));
assert_eq!(iter.next(), Some("French"));
assert_eq!(iter.next(), Some("Italian"));
assert_eq!(iter.next(), Some("Slavic"));
assert_eq!(iter.next(), Some("Polish"));
assert_eq!(iter.next(), Some("Russian"));
assert!(iter.next().is_none());
Source

pub fn ancestors_tokens<'a, T>( self, arena: &'a Arena<T>, ) -> AncestorTokens<'a, T>

Returns an iterator of tokens of ancestor nodes.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let child_token = root_token.append(&mut arena, "Germanic");
let grandchild_token = child_token.append(&mut arena, "English");
let mut ancestors_tokens = grandchild_token.ancestors_tokens(&arena);

assert_eq!(ancestors_tokens.next(), Some(child_token));
assert_eq!(ancestors_tokens.next(), Some(root_token));
assert!(ancestors_tokens.next().is_none());
Source

pub fn preceding_siblings_tokens<'a, T>( self, arena: &'a Arena<T>, ) -> PrecedingSiblingTokens<'a, T>

Returns an iterator of tokens of siblings preceding the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Germanic");
let second_child_token = root_token.append(&mut arena, "Romance");
let third_child_token = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Hellenic");

let mut sibling_tokens = third_child_token.preceding_siblings_tokens(&arena);
assert_eq!(sibling_tokens.next(), Some(second_child_token));
assert_eq!(sibling_tokens.next(), Some(first_child_token));
assert!(sibling_tokens.next().is_none());
Source

pub fn following_siblings_tokens<'a, T>( self, arena: &'a Arena<T>, ) -> FollowingSiblingTokens<'a, T>

Returns an iterator of tokens of siblings following the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Hellenic");

let mut sibling_tokens = second_child_token.following_siblings_tokens(&arena);
assert_eq!(sibling_tokens.next(), Some(third_child_token));
assert_eq!(sibling_tokens.next(), Some(fourth_child_token));
assert!(sibling_tokens.next().is_none());
Source

pub fn children_tokens<'a, T>( self, arena: &'a Arena<T>, ) -> ChildrenTokens<'a, T>

Returns an iterator of tokens of child nodes in the order of insertion.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Hellenic");

let mut children_tokens = root_token.children_tokens(&arena);
assert_eq!(children_tokens.next(), Some(first_child_token));
assert_eq!(children_tokens.next(), Some(second_child_token));
assert_eq!(children_tokens.next(), Some(third_child_token));
assert_eq!(children_tokens.next(), Some(fourth_child_token));
assert!(children_tokens.next().is_none());
Source

pub fn ancestors<'a, T>(self, arena: &'a Arena<T>) -> Ancestors<'a, T>

Returns an iterator of references of ancestor nodes.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let child_token = root_token.append(&mut arena, "Germanic");
let grandchild_token = child_token.append(&mut arena, "Swedish");
let mut ancestors = grandchild_token.ancestors(&arena);

assert_eq!(ancestors.next().unwrap().data, "Germanic");
assert_eq!(ancestors.next().unwrap().data, "Indo-European");
assert!(ancestors.next().is_none());
Source

pub fn preceding_siblings<'a, T>( self, arena: &'a Arena<T>, ) -> PrecedingSiblings<'a, T>

Returns an iterator of references of sibling nodes preceding the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
root_token.append(&mut arena, "Germanic");
let third_child_token = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Celtic");

let mut siblings = third_child_token.preceding_siblings(&arena);
assert_eq!(siblings.next().unwrap().data, "Germanic");
assert_eq!(siblings.next().unwrap().data, "Romance");
assert!(siblings.next().is_none());
Source

pub fn following_siblings<'a, T>( self, arena: &'a Arena<T>, ) -> FollowingSiblings<'a, T>

Returns an iterator of references of sibling nodes following the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
let second_child_token = root_token.append(&mut arena, "Germanic");
root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Hellenic");

let mut siblings = second_child_token.following_siblings(&arena);
assert_eq!(siblings.next().unwrap().data, "Slavic");
assert_eq!(siblings.next().unwrap().data, "Hellenic");
assert!(siblings.next().is_none());
Source

pub fn children<'a, T>(self, arena: &'a Arena<T>) -> Children<'a, T>

Returns an iterator of child node references in the order of insertion.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child_token = root_token.append(&mut arena, "Germanic");
let second_child_token = root_token.append(&mut arena, "Romance");
let third_child_token = root_token.append(&mut arena, "Slavic");
let fourth_child_token = root_token.append(&mut arena, "Celtic");

let mut children = root_token.children(&arena);
assert_eq!(children.next().unwrap().data, "Germanic");
assert_eq!(children.next().unwrap().data, "Romance");
assert_eq!(children.next().unwrap().data, "Slavic");
assert_eq!(children.next().unwrap().data, "Celtic");
assert!(children.next().is_none());
Source

pub fn ancestors_mut<'a, T>( self, arena: &'a mut Arena<T>, ) -> AncestorsMut<'a, T>

Returns an iterator of mutable ancestor node references.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = 1usize;
let (mut arena, root_token) = Arena::with_data(root_data);

let child_token = root_token.append(&mut arena, 2usize);
let grandchild_token = child_token.append(&mut arena, 3usize);
let great_grandchild_token = grandchild_token.append(&mut arena, 4usize);
let ggreat_grandchild_token = great_grandchild_token.append(&mut arena, 5usize);

for x in ggreat_grandchild_token.ancestors_mut(&mut arena) {
    x.data += 2;
}

let mut ancestors = ggreat_grandchild_token.ancestors(&arena);
assert_eq!(ancestors.next().unwrap().data, 6usize);
assert_eq!(ancestors.next().unwrap().data, 5usize);
assert_eq!(ancestors.next().unwrap().data, 4usize);
assert_eq!(ancestors.next().unwrap().data, 3usize);
assert!(ancestors.next().is_none());
Source

pub fn following_siblings_mut<'a, T>( self, arena: &'a mut Arena<T>, ) -> FollowingSiblingsMut<'a, T>

Returns an iterator of mutable references of sibling nodes that follow the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = 1usize;
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, 2usize);
let second_child_token = root_token.append(&mut arena, 3usize);
root_token.append(&mut arena, 4usize);
root_token.append(&mut arena, 5usize);

for x in second_child_token.following_siblings_mut(&mut arena) {
    x.data += 2;
}

let mut children = root_token.children(&arena);
assert_eq!(children.next().unwrap().data, 2usize);
assert_eq!(children.next().unwrap().data, 3usize);
assert_eq!(children.next().unwrap().data, 6usize);
assert_eq!(children.next().unwrap().data, 7usize);
assert!(children.next().is_none());
Source

pub fn preceding_siblings_mut<'a, T>( self, arena: &'a mut Arena<T>, ) -> PrecedingSiblingsMut<'a, T>

Returns an iterator of mutable references of sibling nodes that precede the current node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = 1usize;
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, 2usize);
root_token.append(&mut arena, 3usize);
root_token.append(&mut arena, 4usize);
let fourth_child_token = root_token.append(&mut arena, 5usize);

for x in fourth_child_token.preceding_siblings_mut(&mut arena) {
    x.data += 5;
}

let mut children = root_token.children(&arena);
assert_eq!(children.next().unwrap().data, 7usize);
assert_eq!(children.next().unwrap().data, 8usize);
assert_eq!(children.next().unwrap().data, 9usize);
assert_eq!(children.next().unwrap().data, 5usize);
assert!(children.next().is_none());
Source

pub fn children_mut<'a, T>(self, arena: &'a mut Arena<T>) -> ChildrenMut<'a, T>

Returns an iterator of mutable references of child nodes in the order of insertion.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;

let root_data = 1usize;
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, 2usize);
root_token.append(&mut arena, 3usize);
let third_child_token = root_token.append(&mut arena, 4usize);
root_token.append(&mut arena, 5usize);
let grandchild = third_child_token.append(&mut arena, 10usize);

for x in root_token.children_mut(&mut arena) {
    x.data += 10;
}

let mut children = root_token.children(&arena);
assert_eq!(children.next().unwrap().data, 12);
assert_eq!(children.next().unwrap().data, 13);
assert_eq!(children.next().unwrap().data, 14);
assert_eq!(children.next().unwrap().data, 15);
assert_eq!(arena.get(grandchild).unwrap().data, 10);
assert!(children.next().is_none());
Source

pub fn subtree_tokens<'a, T>( self, arena: &'a Arena<T>, order: TraversalOrder, ) -> SubtreeTokens<'a, T>

Returns an iterator of tokens of subtree nodes of the given node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

let first_child = root_token.append(&mut arena, "Romance");
let second_child = root_token.append(&mut arena, "Germanic");
let third_child = root_token.append(&mut arena, "Slavic");
let first_grandchild = second_child.append(&mut arena, "English");
let second_grandchild = second_child.append(&mut arena, "Icelandic");
let fourth_child = root_token.append(&mut arena, "Celtic");

let mut subtree = root_token.subtree_tokens(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next(), Some(root_token));
assert_eq!(subtree.next(), Some(first_child));
assert_eq!(subtree.next(), Some(second_child));
assert_eq!(subtree.next(), Some(first_grandchild));
assert_eq!(subtree.next(), Some(second_grandchild));
assert_eq!(subtree.next(), Some(third_child));
assert_eq!(subtree.next(), Some(fourth_child));
assert!(subtree.next().is_none());

let mut subtree = second_grandchild.subtree_tokens(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next(), Some(second_grandchild));
assert!(subtree.next().is_none());
Source

pub fn subtree<'a, T>( self, arena: &'a Arena<T>, order: TraversalOrder, ) -> Subtree<'a, T>

Returns an iterator of references of subtree nodes of the given node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = "Indo-European";
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, "Romance");
root_token.append(&mut arena, "Germanic");
let third_child = root_token.append(&mut arena, "Slavic");
root_token.append(&mut arena, "Celtic");
third_child.append(&mut arena, "Polish");
third_child.append(&mut arena, "Slovakian");

let mut subtree = root_token.subtree(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next().unwrap().data, "Indo-European");
assert_eq!(subtree.next().unwrap().data, "Romance");
assert_eq!(subtree.next().unwrap().data, "Germanic");
assert_eq!(subtree.next().unwrap().data, "Slavic");
assert_eq!(subtree.next().unwrap().data, "Polish");
assert_eq!(subtree.next().unwrap().data, "Slovakian");
assert_eq!(subtree.next().unwrap().data, "Celtic");
assert!(subtree.next().is_none());
Source

pub fn subtree_mut<'a, T>( self, arena: &'a mut Arena<T>, order: TraversalOrder, ) -> SubtreeMut<'a, T>

Returns an iterator of mutable references of subtree nodes of the given node.

§Panics:

Panics if the token does not correspond to a node in the arena.

§Examples:
use atree::Arena;
use atree::iter::TraversalOrder;

let root_data = 1usize;
let (mut arena, root_token) = Arena::with_data(root_data);

root_token.append(&mut arena, 2usize);
root_token.append(&mut arena, 3usize);
let third_child = root_token.append(&mut arena, 4usize);
root_token.append(&mut arena, 5usize);
third_child.append(&mut arena, 10usize);
third_child.append(&mut arena, 20usize);

for x in root_token.subtree_mut(&mut arena, TraversalOrder::Pre) {
    x.data += 100;
}

let mut subtree = root_token.subtree(&arena, TraversalOrder::Pre);
assert_eq!(subtree.next().unwrap().data, 101);
assert_eq!(subtree.next().unwrap().data, 102);
assert_eq!(subtree.next().unwrap().data, 103);
assert_eq!(subtree.next().unwrap().data, 104);
assert_eq!(subtree.next().unwrap().data, 110);
assert_eq!(subtree.next().unwrap().data, 120);
assert_eq!(subtree.next().unwrap().data, 105);
assert!(subtree.next().is_none());

Trait Implementations§

Source§

impl Clone for Token

Source§

fn clone(&self) -> Token

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Token

Source§

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

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

impl Hash for Token

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<Token> for Arena<T>

Source§

type Output = Node<T>

The returned type after indexing.
Source§

fn index(&self, index: Token) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<Token> for Arena<T>

Source§

fn index_mut(&mut self, index: Token) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl PartialEq for Token

Source§

fn eq(&self, other: &Token) -> 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 Copy for Token

Source§

impl Eq for Token

Source§

impl StructuralPartialEq for Token

Auto Trait Implementations§

§

impl Freeze for Token

§

impl RefUnwindSafe for Token

§

impl Send for Token

§

impl Sync for Token

§

impl Unpin for Token

§

impl UnwindSafe for Token

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.