[−][src]Struct generic_tree::Node
A single building block to represent an element within a Tree. Itself can contain a list of more Nodes. It should eventually be part of a Tree and it shouldn't be necessary to keep it around on its own.
Implementations
impl<K, V> Node<K, V> where
K: PartialEq, [src]
K: PartialEq,
pub fn new(key: K, value: V) -> Self[src]
Creates a new Node.
Examples
use generic_tree::Node; let node = Node::new("some_key".to_owned(), 42); assert_eq!(node.key(), "some_key"); assert_eq!(node.value(), &42);
pub fn add_child(&mut self, child: Node<K, V>) -> Option<Box<Node<K, V>>>[src]
Add a child Node to the current Node. If there already is a child with the same key, that
child will be returned.
Examples
use generic_tree::Node; let mut parent = Node::new("parent".to_owned(), 2); let child = Node::new("child".to_owned(), 3); assert!(parent.add_child(child).is_none()); let child = Node::new("child".to_owned(), 4); let prev_child = parent.add_child(child).unwrap(); assert_eq!(prev_child.value(), &3);
pub fn add_child_at_path<Q>(
&mut self,
path: &[Q],
child: Node<K, V>
) -> Result<Option<Box<Node<K, V>>>, Error> where
Q: PartialEq<K>, [src]
&mut self,
path: &[Q],
child: Node<K, V>
) -> Result<Option<Box<Node<K, V>>>, Error> where
Q: PartialEq<K>,
Add a child Node at a specific descendent path. If there was already such a child, it will be returned.
Errors
Will return Err if path can't be resolved.
Examples
use generic_tree::Node; let mut grandparent = Node::new("grandparent".to_owned(), 1); let mut parent = Node::new("parent".to_owned(), 2); grandparent.add_child(parent); // Tree structure: // grandparent -> parent assert!(grandparent.get_child(&["parent", "child"]).is_err()); let child = Node::new("child".to_owned(), 3); assert!(grandparent.add_child_at_path(&["parent"], child).is_ok()); assert!(grandparent.get_child(&["parent", "child"]).is_ok());
pub fn get_child<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error> where
Q: PartialEq<K>, [src]
Q: PartialEq<K>,
Get a child node based on a list of keys.
Errors
Will return Err if path can't be resolved.
Examples
use generic_tree::Node; let mut grandparent = Node::new("grandparent".to_owned(), 1); let mut parent = Node::new("parent".to_owned(), 2); let child = Node::new("child".to_owned(), 3); assert!(parent.add_child(child).is_none()); assert!(grandparent.add_child(parent).is_none()); // Tree structure: // grandparent -> parent -> child assert!(grandparent.get_child(&["parent", "child"]).is_ok());
pub fn get_mut_child<Q>(&mut self, path: &[Q]) -> Option<&mut Node<K, V>> where
Q: PartialEq<K>, [src]
Q: PartialEq<K>,
Get mutable a child node based on a list of keys.
Examples
use generic_tree::Node; // Tree structure: // grandparent -> parent -> child assert!(grandparent.get_mut_child(&["parent", "child"]).is_some());
pub fn remove_child<Q>(&mut self, path: &[Q]) -> Result<Box<Node<K, V>>, Error> where
Q: PartialEq<K>, [src]
Q: PartialEq<K>,
Remove a child node based on a list of keys. A boxed Node is returned if found, None
otherwise.
Errors
Will return Err if path can't be resolved.
Examples
use generic_tree::Node; let mut grandparent = Node::new("grandparent".to_owned(), 1); let mut parent = Node::new("parent".to_owned(), 2); let child = Node::new("child".to_owned(), 3); parent.add_child(child); grandparent.add_child(parent); // Tree structure: // grandparent -> parent -> child assert!(grandparent.remove_child(&["parent", "child"]).is_ok()); assert!(grandparent.remove_child(&["parent", "child"]).is_err());
pub fn value(&self) -> &V[src]
Get a reference the value contained within the node.
Examples
use generic_tree::Node; let node = Node::new("some_key".to_owned(), 42); assert_eq!(node.value(), &42);
pub fn mut_value(&mut self) -> &mut V[src]
Get a muteable reference to the value contained within the node.
Examples
use generic_tree::Node; let mut node = Node::new("some_key".to_owned(), 42); let mut value = node.mut_value(); assert_eq!(value, &42); *value = 43; assert_eq!(node.value(), &43);
pub fn key(&self) -> &K[src]
Get a reference to the key of the node.
Examples
use generic_tree::Node; let mut node = Node::new("some_key".to_owned(), 42); assert_eq!(node.key(), "some_key");
Trait Implementations
impl<K, V> PartialEq<K> for Node<K, V> where
K: PartialEq, [src]
K: PartialEq,
impl<K, V> PartialEq<Node<K, V>> for Node<K, V> where
K: PartialEq, [src]
K: PartialEq,
Auto Trait Implementations
impl<K, V> RefUnwindSafe for Node<K, V> where
K: RefUnwindSafe,
V: RefUnwindSafe,
K: RefUnwindSafe,
V: RefUnwindSafe,
impl<K, V> Send for Node<K, V> where
K: Send,
V: Send,
K: Send,
V: Send,
impl<K, V> Sync for Node<K, V> where
K: Sync,
V: Sync,
K: Sync,
V: Sync,
impl<K, V> Unpin for Node<K, V> where
K: Unpin,
V: Unpin,
K: Unpin,
V: Unpin,
impl<K, V> UnwindSafe for Node<K, V> where
K: UnwindSafe,
V: UnwindSafe,
K: UnwindSafe,
V: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,