Struct Node

Source
pub struct Node<T: Debug + Clone> {
    pub inner: Rc<HedelCell<NodeInner<T>>>,
}
Expand description

Wraps the inner value with an Rc<HedelCell<_>> pointer. allowing for multiple owners and a mutable NodeInner

Fields§

§inner: Rc<HedelCell<NodeInner<T>>>

Implementations§

Source§

impl<T: Debug + Clone> Node<T>

Source

pub fn new(content: T) -> Self

Default constructor. Notice how it builds a stand-alone node, not pointing to any parent, any sibling and any child, but owning the content

Source

pub fn downgrade(&self) -> WeakNode<T>

A WeakNode has to be built by downgrading Node following the same logic to get a Weak from a Rc

Source

pub fn try_get(&self) -> Result<RefHedel<'_, NodeInner<T>>, HedelError>

Get access to NodeInner or return HedelError in case the runtime borrow checker in HedelCell doesn’t allow to get a shared reference.

Source

pub fn get(&self) -> RefHedel<'_, NodeInner<T>>

Get access to NodeInner or panic! in case the runtime borrow checker in HedelCell doesn’t allow to get a shared reference.

Source

pub fn try_get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>

Get mutable access to NodeInner or return HedelError in case the runtime borrow checker in HedelCell doesn’t allow to get a mutable reference.

Source

pub fn get_mut(&self) -> RefMutHedel<'_, NodeInner<T>>

Get mutable access to NodeInner or panic! in case the runtime borrow checker in HedelCell doesn’t allow to get a mutable reference.

Source

pub fn next(&self) -> Option<Node<T>>

Get the next Node in horizontal direction

Source

pub fn prev(&self) -> Option<Node<T>>

Get the previous Node in horizontal direction by upgrading it.

Source

pub fn parent(&self) -> Option<Node<T>>

Get the parent Node in vertical direction by upgrading it.

Source

pub fn list(&self) -> Option<List<T>>

if currently under a NodeList, returns it.

Source

pub fn child(&self) -> Option<Node<T>>

Get the first child Node in vertical direction.

Source

pub fn to_content(self) -> T

Source

pub fn free(&self)

Re-set the parent, next and prev fields on the Node. WARNING: this is meant to be used by NodeCollection::free after the HedelDetach::detach_preserve function. Refer to it’s documentation for an usage example.

If you want to detach a single Node while iterating, most of the times you can simply break the loop and use HedelDetach::detach. WARNING: using this function instead of HedelDetach::detach might break the linked list.

Trait Implementations§

Source§

impl<T: Debug + Clone> AppendNode<T> for Node<T>

Source§

fn append_next(&self, node: Node<T>)

Inserts a new node right after &self.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

fn main() {
	let node = node!(1, node!(2));
	let two = node.child().unwrap();                            	
	two.append_next(node!(3));                         	
	assert_eq!(node.get_last_child().unwrap().to_content(), 3);	
}	
Source§

fn append_prev(&self, node: Node<T>)

Inserts a new node right before &self.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

fn main() {
	let node = node!(1, node!(2));
	let two = node.child().unwrap();
	two.append_prev(node!(3));
	assert_eq!(node.child().unwrap().to_content(), 3);
}
Source§

fn append_child(&self, node: Node<T>)

Inserts a new node right after the last child of &self.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

fn main() {
	let node = node!(1, node!(2));
	node.append_child(node!(3));
	println!("{}", node.get_last_child().unwrap().to_content());
}
Source§

impl<T: Debug + Clone> Clone for Node<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Clone, I: CompareNode<T>> CollectNode<T, I> for Node<T>

Source§

fn collect_siblings(&self, ident: &I) -> NodeCollection<T>

Given an identifier of type implementing CompareNode this iterates over all the nodes in the linked list horizontally ( iterates over the siblings, previous and next ), and compare every node. The nodes satisfying the identifier get collected into a NodeCollection.

Source§

fn collect_children(&self, ident: &I) -> NodeCollection<T>

Given an identifier of type implementing CompareNode this iterates over all the nodes that stand lower and deeper in the linked list. Every child satysfying the identifier get collected into a NodeCollection

Source§

fn collect_linked_list(&self, ident: &I) -> NodeCollection<T>

Given an identifier of type implementing CompareNode this iterates over all the nodes in the linked list both horizontally and vertically ( iterates horizontally in each hierarchical level, up to the top parent and down to the deepest child also iterating vertically and horizontally for each layer of the children ). The nodes satisfying the identifier get collected into a NodeCollection.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;
 
pub enum NumIdent {
	  Equal(i32),
	  BiggerThan(i32),
	  SmallerThan(i32)
}

impl CompareNode<i32> for NumIdent {
	fn compare(&self, node: &Node<i32>) -> bool {
		match &self {
		  NumIdent::Equal(n) => {
			as_content!(node, |content| {
			  content == *n
			})
	   	  },
		  NumIdent::BiggerThan(n) => {
			as_content!(node, |content| {
			  content > *n
			})
		  },
		  NumIdent::SmallerThan(n) => {
			as_content!(node, |content| {
				content < *n
			})
		  }
	  }
  }
}

fn main() {
	let node = node!(1,
		node!(8),
		node!(3),
		node!(7),
		node!(6, node!(3))
	);

	// retrive any child
	let a = node.get_last_child().unwrap();

	let collection = a.collect_linked_list(&NumIdent::SmallerThan(5));

	for node in collection.into_iter() {
		println!("{}", node.to_content());
	}
}
Source§

impl<T: Debug + Debug + Clone> Debug for Node<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Debug + Clone> DetachNode<T> for Node<T>

Source§

fn detach(&self)

Detaches a single node from the linked list by fixing the pointers between the parent, the previous and next siblings. This also detaches all the children of the Node, which will only remain linked with the node itself. WARNING: This also re-sets the pointers in the node itself to None. So when you are detecting nodes in a linked-list and detaching them, you cant iterate over them using this method as it would break the loop. Use detach_preserve instead.

Source§

fn detach_preserve(&self, vec: &mut NodeCollection<T>)

Detaches a single node from the linked list like detach, but doesn’t re-set the pointers inside the Node. This should only be used when you have to iterate over a linked list and detach some Nodes. You should create a vector to store the detached nodes, and iterate over them only when the while loop is compleated, re-setting the parent, prev, next fields to None.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

pub enum NumIdent {
     Equal(i32),
     BiggerThan(i32),
     SmallerThan(i32)
}

impl CompareNode<i32> for NumIdent {
   fn compare(&self, node: &Node<i32>) -> bool {
       match &self {
         NumIdent::Equal(n) => {
           as_content!(node, |content| {
               content == *n
           })
         },
         NumIdent::BiggerThan(n) => {
           as_content!(node, |content| {
            	content > *n
           })
         },
         NumIdent::SmallerThan(n) => {
           as_content!(node, |content| {
            	content < *n
           })
         }
     }
 }
}

fn main() {
   	let list = list!(
   		node!(1),
   		node!(2),
   		node!(3),
   		node!(4),
   		node!(5),
   		node!(6)
   	);

   	let ident = NumIdent::SmallerThan(4);

   	let mut detached_nodes = NodeCollection::<i32>::new();
   
   	// possible algorithm to detach all the nodes smaller than 4 in a linked list.
   	let mut next: Node<i32> = list.first().unwrap();

   	/* do */ {
   		if ident.compare(&next) {
   			next.detach_preserve(&mut detached_nodes);
   		}
   	} while let Some(n) = next.next() {

   		next = n;

   		if ident.compare(&next) {
   			next.detach_preserve(&mut detached_nodes);
   		}
   	}

   	// this will finally re-set to None every pointer in the collected
   	// nodes.
   	detached_nodes.free();
}
Source§

impl<T: Debug + Clone, I: CompareNode<T>> FindNode<T, I> for Node<T>

Source§

fn find_next(&self, ident: &I) -> Option<Node<T>>

Get the first Node in the linked list, at the same depth-level of &self and coming after it, matching the identifier. This guarantees to actually retrive the closest Node.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

pub enum NumIdent {
     Equal(i32),
     BiggerThan(i32),
     SmallerThan(i32)
}

impl CompareNode<i32> for NumIdent {
    fn compare(&self, node: &Node<i32>) -> bool {
        match &self {
          NumIdent::Equal(n) => {
              as_content!(node, |content| {
                 content == *n
              })
           },
          NumIdent::BiggerThan(n) => {
            as_content!(node, |content| {
              content > *n
            })
          },
          NumIdent::SmallerThan(n) => {
            as_content!(node, |content| {
                content < *n
            })
          }
      }
  }
}

fn main() {

	let node = node!(33,
		node!(1),
		node!(34),
		node!(66)
	); 
		
	let one = node.child().unwrap();
	assert_eq!(
		one.find_next(&NumIdent::BiggerThan(50)).unwrap().to_content(),
		66
	); 
}
Source§

fn find_prev(&self, ident: &I) -> Option<Node<T>>

Get the first Node in the linked list, at the same depth-level of &self and coming before it, matching the identifier. This guarantees to actually retrive the closest Node.

Source§

fn find_linked_list(&self, ident: &I) -> Option<Node<T>>

Get a Node somewhere in the linked list matching the identifier. WARNING: it’s not guaranteed to retrive the closest Node. Only use when you don’t care about which node is retrived as long as it matches the identifier or when you are 100% sure that there isn’t more than one Node satisfying the identifier in the linked list.

Source§

fn find_child(&self, ident: &I) -> Option<Node<T>>

Get the first child Node of &self in the linked list matching the identifier. WARNING: it’s not guaranteed to retrive the closest Node. Only use when you don’t care about which node is retrived as long as it matches the identifier or when you are 100% sure that there isn’t more than one Node satisfying the identifier in the children.

Source§

fn find_sibling(&self, ident: &I) -> Option<Node<T>>

In the case you can’t know if the Node you are looking for comes before or after, here’s a combination of the two previous methods. Always prefer using HedelFind::find_next and HedelFind::find_prev when you know the position of the Node, as they might be faster.

Source§

impl<T: Debug + Clone> GetNode<T> for Node<T>

Source§

fn get_first_sibling(&self) -> Option<Node<T>>

Get the first Node in the linked list at the same depth level of &self. If None is returned, &self is the first Node at that depth level.

Source§

fn get_last_sibling(&self) -> Option<Node<T>>

Get the last Node in the linked list at the same depth level of &self. If None is returned, &self is the last Node at that depth level.

Source§

fn get_last_child(&self) -> Option<Node<T>>

Get the last child Node of &self If None is returned, &self doesn’t have any children. NOTE: use &self.child() to get the first Node.

Source§

impl<T: Debug + Clone> InsertNode<T> for Node<T>

Source§

fn insert_sibling(&self, position: usize, node: Node<T>)

Inserts a new node at the same depth-level of &self and at the given position.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

fn main() {
	let mut node = node!(1, node!(2), node!(4));

	let two = node.child().unwrap();
	two.insert_sibling(23, node!(3));

	// if the position is bigger than the length, the node gets placed at the end
	let three = node.get_last_child().unwrap();
	println!("{}", three.to_content()); // prints 3
}
Source§

fn insert_child(&self, position: usize, node: Node<T>)

Inserts a new node to the childrenl of &self and at the given position.

§Example
use hedel_rs::prelude::*;
use hedel_rs::*;

fn main() {
	let mut node = node!(1, node!(2), node!(4));

	node.insert_child(2, node!(3));

	let three = node.get_last_child().unwrap();
	println!("{}", three.to_content()); // prints 3
}

Auto Trait Implementations§

§

impl<T> Freeze for Node<T>

§

impl<T> !RefUnwindSafe for Node<T>

§

impl<T> !Send for Node<T>

§

impl<T> !Sync for Node<T>

§

impl<T> Unpin for Node<T>

§

impl<T> !UnwindSafe for Node<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.