pub struct NodeMut<'a, T> { /* private fields */ }
Expand description
A mutable reference to a given Node
’s data and its relatives.
Implementations§
Source§impl<'a, T> NodeMut<'a, T>
impl<'a, T> NodeMut<'a, T>
Sourcepub fn node_id(&self) -> NodeId
pub fn node_id(&self) -> NodeId
Returns the NodeId
that identifies this Node
in the tree.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let root_id = tree.root_id().expect("root doesn't exist?");
let root = tree.root_mut().expect("root doesn't exist?");
let root_id_again = root.node_id();
assert_eq!(root_id_again, root_id);
Sourcepub fn data(&mut self) -> &mut T
pub fn data(&mut self) -> &mut T
Returns a mutable reference to the data contained by the given Node
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
let data = root.data();
assert_eq!(data, &mut 1);
*data = 3;
assert_eq!(data, &mut 3);
Sourcepub fn parent(&mut self) -> Option<NodeMut<'_, T>>
pub fn parent(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to this Node
’s parent. Returns a Some
-value containing
the NodeMut
if this Node
has a parent; otherwise returns a None
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
assert!(root.parent().is_none());
Sourcepub fn prev_sibling(&mut self) -> Option<NodeMut<'_, T>>
pub fn prev_sibling(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to this Node
’s previous sibling. Returns a Some
-value
containing the NodeMut
if this Node
has a previous sibling; otherwise returns a None
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
assert!(root.prev_sibling().is_none());
Sourcepub fn next_sibling(&mut self) -> Option<NodeMut<'_, T>>
pub fn next_sibling(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to this Node
’s next sibling. Returns a Some
-value
containing the NodeMut
if this Node
has a next sibling; otherwise returns a None
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
assert!(root.next_sibling().is_none());
Sourcepub fn first_child(&mut self) -> Option<NodeMut<'_, T>>
pub fn first_child(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to this Node
’s first child. Returns a Some
-value
containing the NodeMut
if this Node
has a first child; otherwise returns a None
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
assert!(root.first_child().is_none());
Sourcepub fn last_child(&mut self) -> Option<NodeMut<'_, T>>
pub fn last_child(&mut self) -> Option<NodeMut<'_, T>>
Returns a NodeMut
pointing to this Node
’s last child. Returns a Some
-value
containing the NodeMut
if this Node
has a last child; otherwise returns a None
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
assert!(root.last_child().is_none());
Sourcepub fn append(&mut self, data: T) -> NodeMut<'_, T>
pub fn append(&mut self, data: T) -> NodeMut<'_, T>
Appends a new Node
as this Node
’s last child (and first child if it has none).
Returns a NodeMut
pointing to the newly added Node
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 2);
assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 2);
let mut child = root.first_child().unwrap();
assert!(child.parent().is_some());
assert_eq!(child.parent().unwrap().data(), &mut 1);
Examples found in repository?
5fn main() {
6 // "hello"
7 // / \
8 // "world" "trees"
9 // |
10 // "are"
11 // |
12 // "cool"
13
14 let mut tree = TreeBuilder::new().with_root("hello").build();
15 let root_id = tree.root_id().expect("root doesn't exist?");
16 let mut hello = tree.get_mut(root_id).unwrap();
17
18 hello.append("world");
19 hello.append("trees").append("are").append("cool");
20}
More examples
5fn main() {
6 let mut tree = TreeBuilder::new().with_root(0).build();
7 let mut root = tree.root_mut().unwrap();
8 {
9 let mut one = root.append(1);
10 let mut two = one.append(2);
11 two.append(3);
12 two.append(4);
13 }
14 {
15 let mut five = root.append(5);
16 five.append(6).append(7);
17 five.append(8);
18 }
19 root.append(9);
20
21 let mut s = String::new();
22 // 0
23 // ├── 1
24 // │ └── 2
25 // │ ├── 3
26 // │ └── 4
27 // ├── 5
28 // │ ├── 6
29 // │ │ └── 7
30 // │ └── 8
31 // └── 9
32 tree.write_formatted(&mut s).unwrap();
33 print!("{}", s);
34}
Sourcepub fn prepend(&mut self, data: T) -> NodeMut<'_, T>
pub fn prepend(&mut self, data: T) -> NodeMut<'_, T>
Prepends a new Node
as this Node
’s first child (and last child if it has none).
Returns a NodeMut
pointing to the newly added Node
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
root.prepend(2);
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 2);
assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 2);
let mut child = root.first_child().unwrap();
assert!(child.parent().is_some());
assert_eq!(child.parent().unwrap().data(), &mut 1);
Sourcepub fn remove_first(&mut self, behavior: RemoveBehavior) -> Option<T>
pub fn remove_first(&mut self, behavior: RemoveBehavior) -> Option<T>
Remove the first child of this Node
and return the data that child contained.
Returns a Some
-value if this Node
has a child to remove; returns a None
-value
otherwise.
Children of the removed Node
can either be dropped with DropChildren
or orphaned with
OrphanChildren
.
use slab_tree::tree::TreeBuilder;
use slab_tree::behaviors::RemoveBehavior::*;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
root.append(3);
let two = root.remove_first(DropChildren);
assert!(two.is_some());
assert_eq!(two.unwrap(), 2);
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 3);
assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 3);
Sourcepub fn remove_last(&mut self, behavior: RemoveBehavior) -> Option<T>
pub fn remove_last(&mut self, behavior: RemoveBehavior) -> Option<T>
Remove the first child of this Node
and return the data that child contained.
Returns a Some
-value if this Node
has a child to remove; returns a None
-value
otherwise.
Children of the removed Node
can either be dropped with DropChildren
or orphaned with
OrphanChildren
.
use slab_tree::tree::TreeBuilder;
use slab_tree::behaviors::RemoveBehavior::*;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
root.append(3);
let three = root.remove_last(DropChildren);
assert!(three.is_some());
assert_eq!(three.unwrap(), 3);
assert!(root.first_child().is_some());
assert_eq!(root.first_child().unwrap().data(), &mut 2);
assert!(root.last_child().is_some());
assert_eq!(root.last_child().unwrap().data(), &mut 2);
Sourcepub fn as_ref(&self) -> NodeRef<'_, T>
pub fn as_ref(&self) -> NodeRef<'_, T>
Returns a NodeRef
pointing to this NodeMut
.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
let root = root.as_ref();
assert_eq!(root.data(), &1);
Sourcepub fn swap_next_sibling(&mut self) -> bool
pub fn swap_next_sibling(&mut self) -> bool
Exchange positions with the next sibling.
Returns true if swapped with a next sibling, returns false if this was already the last sibling.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let two_id = {
let mut root = tree.root_mut().expect("root doesn't exist?");
let two_id = root.append(2).node_id();
root.append(3);
root.append(4);
two_id
};
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![2, 3, 4]);
assert!(tree.get_mut(two_id).unwrap().swap_next_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![3, 2, 4]);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
3);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
4);
assert!(tree.get_mut(two_id).unwrap().swap_next_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![3, 4, 2]);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
3);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
2);
assert!(!tree.get_mut(two_id).unwrap().swap_next_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![3, 4, 2]);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
3);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
2);
Sourcepub fn swap_prev_sibling(&mut self) -> bool
pub fn swap_prev_sibling(&mut self) -> bool
Exchange positions with the previous sibling.
Returns true if swapped with a previous sibling, returns false if this was already the first sibling.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let four_id = {
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
root.append(3);
let four_id = root.append(4).node_id();
four_id
};
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![2, 3, 4]);
assert!(tree.get_mut(four_id).unwrap().swap_prev_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![2, 4, 3]);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
2);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
3);
assert!(tree.get_mut(four_id).unwrap().swap_prev_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![4, 2, 3]);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
4);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
3);
assert!(!tree.get_mut(four_id).unwrap().swap_prev_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![4, 2, 3]);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
4);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
3);
Sourcepub fn make_last_sibling(&mut self) -> bool
pub fn make_last_sibling(&mut self) -> bool
Moves this node to the last sibling position.
Returns false if the node was already the last sibling.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let two_id = {
let mut root = tree.root_mut().expect("root doesn't exist?");
let two_id = root.append(2).node_id();
root.append(3);
root.append(4);
two_id
};
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![2, 3, 4]);
assert!(tree.get_mut(two_id).unwrap().make_last_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![3, 4, 2]);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
3);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
2);
assert!(!tree.get_mut(two_id).unwrap().make_last_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![3, 4, 2]);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
3);
assert_eq!(
*tree.get(two_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
2);
Sourcepub fn make_first_sibling(&mut self) -> bool
pub fn make_first_sibling(&mut self) -> bool
Moves this node to the first sibling position.
Returns false if the node was already the first sibling.
use slab_tree::tree::TreeBuilder;
let mut tree = TreeBuilder::new().with_root(1).build();
let four_id = {
let mut root = tree.root_mut().expect("root doesn't exist?");
root.append(2);
root.append(3);
root.append(4).node_id()
};
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![2, 3, 4]);
assert!(tree.get_mut(four_id).unwrap().make_first_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![4, 2, 3]);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
4);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
3);
assert!(!tree.get_mut(four_id).unwrap().make_first_sibling());
assert_eq!(
tree.root().unwrap().children().map(|child_ref| *child_ref.data())
.collect::<Vec<i32>>(),
vec![4, 2, 3]);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().first_child().unwrap()
.data(),
4);
assert_eq!(
*tree.get(four_id).unwrap().parent().unwrap().last_child().unwrap()
.data(),
3);