Struct TreeBuilder

Source
pub struct TreeBuilder<T> { /* private fields */ }
Expand description

A Tree builder. Provides more control over how a Tree is created.

Implementations§

Source§

impl<T> TreeBuilder<T>

Source

pub fn new() -> TreeBuilder<T>

Creates a new TreeBuilder with the default settings.

use slab_tree::tree::TreeBuilder;

let _tree_builder = TreeBuilder::new();
Examples found in repository?
examples/basic.rs (line 14)
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 6)
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 with_root(self, root: T) -> TreeBuilder<T>

Sets the root Node of the TreeBuilder.

use slab_tree::tree::TreeBuilder;

let _tree_builder = TreeBuilder::new().with_root(1);
Examples found in repository?
examples/basic.rs (line 14)
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 6)
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 with_capacity(self, capacity: usize) -> TreeBuilder<T>

Sets the capacity of the TreeBuilder.

This can be used to pre-allocate space in the Tree if you know you’ll be adding a large number of Nodes to the Tree.

use slab_tree::tree::TreeBuilder;

let _tree_builder = TreeBuilder::new().with_capacity(10);
Source

pub fn build(self) -> Tree<T>

Build a Tree based upon the current settings in the TreeBuilder.

use slab_tree::tree::TreeBuilder;

let _tree = TreeBuilder::new().with_root(1).with_capacity(10).build();
Examples found in repository?
examples/basic.rs (line 14)
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 6)
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}

Trait Implementations§

Source§

impl<T> Default for TreeBuilder<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TreeBuilder<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for TreeBuilder<T>
where T: RefUnwindSafe,

§

impl<T> Send for TreeBuilder<T>
where T: Send,

§

impl<T> Sync for TreeBuilder<T>
where T: Sync,

§

impl<T> Unpin for TreeBuilder<T>
where T: Unpin,

§

impl<T> UnwindSafe for TreeBuilder<T>
where T: UnwindSafe,

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.