[][src]Struct generic_tree::Tree

pub struct Tree<K, V> { /* fields omitted */ }

A Tree represents and owns a collection of Nodes. It should be the go-to point for interacting with elements wihtin the structure.

Implementations

impl<K, V> Tree<K, V> where
    K: PartialEq
[src]

pub fn init(root: Node<K, V>) -> Self[src]

Create a new tree, given the root Node root.

pub fn get_node<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error> where
    Q: PartialEq<K>, 
[src]

Get a reference to a specific Node from the tree, resolved by the list provided by path.

Errors

Returns an Err if path doesn't resolve to a Node.

Examples

let mut root = Node::new("root", 1);
let child = Node::new("child", 2);
root.add_child(child);

let mut tree = Tree::init(root);

assert!(tree.get_node(&["root"]).is_ok());
assert!(tree.get_node(&["root", "child"]).is_ok());
assert!(tree.get_node(&["boot"]).is_err());
assert!(tree.get_node(&["root", "child", "noop"]).is_err());

pub fn get_mut_node<Q>(&mut self, path: &[Q]) -> Option<&mut Node<K, V>> where
    Q: PartialEq<K>, 
[src]

Get a mutable reference to a specific Node from the tree, resolved by the list provided by path.

Errors

Returns an Err if path doesn't resolve to a Node.

Examples

use generic_tree::{Tree, Node};

let mut root = Node::new("root", 1);
let child = Node::new("child", 2);
root.add_child(child);

let mut tree = Tree::init(root);

let mut node = tree.get_mut_node(&["root", "child"]).unwrap();
assert_eq!(node.value(), &2);

*node.mut_value() = 42;

pub fn add_node<Q>(
    &mut self,
    path: &[Q],
    node: Node<K, V>
) -> Result<Option<Box<Node<K, V>>>, Error> where
    Q: PartialEq<K>, 
[src]

Add a Node as a child to the Node resolved by path. If there was already a Node with the same key, that old Node is returned.

Errors

Returns an Err if path doesn't resolve to a Node.

Examples

let mut root = Node::new("root", 1);
let mut tree = Tree::init(root);

let child = Node::new("child", 2);
tree.add_node(&["root"], child);

pub fn remove_node<Q>(&mut self, path: &[Q]) -> Result<Box<Node<K, V>>, Error> where
    Q: PartialEq<K>, 
[src]

Remove a Node from the tree resolved by path.

Errors

Returns an Err if path doesn't resolve to a Node.

Examples

let mut root = Node::new("root", 1);
let child = Node::new("child", 2);
root.add_child(child);
let mut tree = Tree::init(root);

assert!(tree.remove_node(&["root", "child"]).is_ok());

Auto Trait Implementations

impl<K, V> RefUnwindSafe for Tree<K, V> where
    K: RefUnwindSafe,
    V: RefUnwindSafe

impl<K, V> Send for Tree<K, V> where
    K: Send,
    V: Send

impl<K, V> Sync for Tree<K, V> where
    K: Sync,
    V: Sync

impl<K, V> Unpin for Tree<K, V> where
    K: Unpin,
    V: Unpin

impl<K, V> UnwindSafe for Tree<K, V> where
    K: UnwindSafe,
    V: 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.