[][src]Struct slab_tree::tree::Tree

pub struct Tree<T> { /* fields omitted */ }

A tree structure containing Nodes.

Methods

impl<T> Tree<T>[src]

pub fn new() -> Tree<T>[src]

Creates a new Tree with a capacity of 0.

use slab_tree::tree::Tree;

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

pub fn set_root(&mut self, root: T) -> NodeId[src]

Sets the "root" of the Tree to be root.

If there is already a "root" node in the Tree, that node is shifted down and the new one takes its place.

use slab_tree::tree::Tree;

let mut tree = Tree::new();

let root_id = tree.set_root(1);

assert_eq!(tree.root_id().unwrap(), root_id);

pub fn capacity(&self) -> usize[src]

Returns the Tree's current capacity. Capacity is defined as the number of times new Nodes can be added to the Tree before it must allocate more memory.

use slab_tree::tree::Tree;

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

assert_eq!(tree.capacity(), 0);

pub fn root_id(&self) -> Option<NodeId>[src]

Returns the NodeId of the root node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let root_id = tree.root_id().expect("root doesn't exist?");

assert_eq!(tree.get(root_id).unwrap().data(), &1);

pub fn root(&self) -> Option<NodeRef<T>>[src]

Returns a NodeRef pointing to the root Node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let root = tree.root().expect("root doesn't exist?");

assert_eq!(root.data(), &1);

pub fn root_mut(&mut self) -> Option<NodeMut<T>>[src]

Returns a NodeMut pointing to the root Node of the Tree.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);

let mut root = tree.root_mut().expect("root doesn't exist?");
assert_eq!(root.data(), &mut 1);

*root.data() = 2;
assert_eq!(root.data(), &mut 2);

pub fn get(&self, node_id: NodeId) -> Option<NodeRef<T>>[src]

Returns the NodeRef pointing to the Node that the given NodeId identifies. If the NodeId in question points to nothing (or belongs to a different Tree) a None-value will be returned; otherwise, a Some-value will be returned.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");

let root = tree.get(root_id);
assert!(root.is_some());

let root = root.unwrap();
assert_eq!(root.data(), &1);

pub fn get_mut(&mut self, node_id: NodeId) -> Option<NodeMut<T>>[src]

Returns the NodeMut pointing to the Node that the given NodeId identifies. If the NodeId in question points to nothing (or belongs to a different Tree) a None-value will be returned; otherwise, a Some-value will be returned.

use slab_tree::tree::Tree;

let mut tree = Tree::new();
tree.set_root(1);
let root_id = tree.root_id().expect("root doesn't exist?");

let root = tree.get_mut(root_id);
assert!(root.is_some());

let mut root = root.unwrap();

*root.data() = 2;
assert_eq!(root.data(), &mut 2);

pub fn remove(&mut self, node_id: NodeId, behavior: RemoveBehavior) -> Option<T>[src]

Remove a Node by its NodeId and return the data that it contained. Returns a Some-value if the Node exists; 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 two_id = {
    let mut root = tree.root_mut().expect("root doesn't exist?");
    let two_id = root.append(2).node_id();
    root.append(3);
    two_id
};

let two = tree.remove(two_id, DropChildren);

assert!(two.is_some());
assert_eq!(two.unwrap(), 2);

let root = tree.root().expect("root doesn't exist?");
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);

impl<T: Debug> Tree<T>[src]

pub fn write_formatted<W: Write>(&self, w: &mut W) -> Result[src]

Write formatted tree representation and nodes with debug formatting.

Example:

use slab_tree::tree::TreeBuilder;

let mut tree = TreeBuilder::new().with_root(0).build();
let mut root = tree.root_mut().unwrap();
root.append(1)
    .append(2);
root.append(3);
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "\
0
├── 1
│   └── 2
└── 3
");

Writes nothing if the tree is empty.

use slab_tree::tree::TreeBuilder;

let tree = TreeBuilder::<i32>::new().build();
let mut s = String::new();
tree.write_formatted(&mut s).unwrap();
assert_eq!(&s, "");

Trait Implementations

impl<T: Debug> Debug for Tree<T>[src]

impl<T> Default for Tree<T>[src]

impl<T: PartialEq> PartialEq<Tree<T>> for Tree<T>[src]

impl<T> StructuralPartialEq for Tree<T>[src]

Auto Trait Implementations

impl<T> RefUnwindSafe for Tree<T> where
    T: RefUnwindSafe

impl<T> Send for Tree<T> where
    T: Send

impl<T> Sync for Tree<T> where
    T: Sync

impl<T> Unpin for Tree<T> where
    T: Unpin

impl<T> UnwindSafe for Tree<T> where
    T: UnwindSafe

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

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

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

type Error = Infallible

The type returned in the event of a conversion error.

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

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

The type returned in the event of a conversion error.