Struct NodeMut

Source
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>

Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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());
Source

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());
Source

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?
examples/basic.rs (line 18)
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
Hide additional examples
examples/formatted.rs (line 9)
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}
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for NodeMut<'a, T>

Source§

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

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

impl<'a, T: PartialEq> PartialEq for NodeMut<'a, T>

Source§

fn eq(&self, other: &NodeMut<'a, T>) -> 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<'a, T> StructuralPartialEq for NodeMut<'a, T>

Auto Trait Implementations§

§

impl<'a, T> Freeze for NodeMut<'a, T>

§

impl<'a, T> RefUnwindSafe for NodeMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for NodeMut<'a, T>
where T: Send,

§

impl<'a, T> Sync for NodeMut<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for NodeMut<'a, T>

§

impl<'a, T> !UnwindSafe for NodeMut<'a, T>

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.