Skip to main content

Node

Struct Node 

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

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§

Source§

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

Source

pub fn new(key: K, value: V) -> Self

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

pub fn add_child(&mut self, child: Node<K, V>) -> Option<Box<Node<K, V>>>

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

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>,

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

pub fn get_child<Q>(&self, path: &[Q]) -> Result<&Node<K, V>, Error>
where 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());
Source

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

Get mutable a child node based on a list of keys.

§Errors

Will return Err if path can’t be resolved.

§Examples
use generic_tree::Node;



// Tree structure:
// grandparent -> parent -> child
assert!(grandparent.get_mut_child(&["parent", "child"]).is_ok());
Source

pub fn remove_child<Q>(&mut self, path: &[Q]) -> Result<Box<Node<K, V>>, Error>
where 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());
Source

pub fn value(&self) -> &V

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

pub fn mut_value(&mut self) -> &mut V

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

pub fn key(&self) -> &K

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§

Source§

impl<K, V> PartialEq<K> for Node<K, V>
where K: PartialEq,

Source§

fn eq(&self, other: &K) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<K, V> PartialEq for Node<K, V>
where K: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<K, V> Freeze for Node<K, V>
where K: Freeze, V: Freeze,

§

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

§

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

§

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

§

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

§

impl<K, V> UnsafeUnpin for Node<K, V>
where K: UnsafeUnpin, V: UnsafeUnpin,

§

impl<K, V> UnwindSafe for Node<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.