Skip to main content

Tree

Struct Tree 

Source
pub struct Tree<K, V> { /* private fields */ }
Expand description

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

Implementations§

Source§

impl<K, V> Tree<K, V>
where K: PartialEq,

Source

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

Create a new tree, given the root Node root.

Source

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

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

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

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

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

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

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

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> Freeze for Tree<K, V>
where K: Freeze, V: Freeze,

§

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

§

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> UnsafeUnpin for Tree<K, V>
where K: UnsafeUnpin, V: UnsafeUnpin,

§

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