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,
impl<K, V> Tree<K, V>where
K: PartialEq,
Sourcepub fn get_node<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error>where
Q: PartialEq<K>,
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());Sourcepub fn get_mut_node<Q>(&mut self, path: &[Q]) -> Result<&mut Node<K, V>, Error>where
Q: PartialEq<K>,
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;Sourcepub fn add_node<Q>(
&mut self,
path: &[Q],
node: Node<K, V>,
) -> Result<Option<Box<Node<K, V>>>, Error>where
Q: PartialEq<K>,
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);Auto Trait Implementations§
impl<K, V> Freeze for Tree<K, V>
impl<K, V> RefUnwindSafe for Tree<K, V>where
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Tree<K, V>
impl<K, V> Sync for Tree<K, V>
impl<K, V> Unpin for Tree<K, V>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more