ego_tree

Struct NodeMut

Source
pub struct NodeMut<'a, T: 'a> { /* private fields */ }
Expand description

Node mutator.

Implementations§

Source§

impl<'a, T: 'a> NodeMut<'a, T>

Source

pub fn sort(&mut self)
where T: Ord,

Sort children by value in ascending order.

§Examples
use ego_tree::tree;

let mut tree = tree!('a' => { 'd', 'c', 'b' });
tree.root_mut().sort();
assert_eq!(
    vec![&'b', &'c', &'d'],
    tree.root()
        .children()
        .map(|n| n.value())
        .collect::<Vec<_>>(),
);
Source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut(NodeRef<'_, T>, NodeRef<'_, T>) -> Ordering,

Sort children by NodeRef in ascending order using a comparison function.

§Examples
use ego_tree::tree;

let mut tree = tree!('a' => { 'c', 'd', 'b' });
tree.root_mut().sort_by(|a, b| b.value().cmp(a.value()));
assert_eq!(
    vec![&'d', &'c', &'b'],
    tree.root()
        .children()
        .map(|n| n.value())
        .collect::<Vec<_>>(),
);

// Example for sort_by_id.
tree.root_mut().sort_by(|a, b| a.id().cmp(&b.id()));
assert_eq!(
    vec![&'c', &'d', &'b'],
    tree.root()
        .children()
        .map(|n| n.value())
        .collect::<Vec<_>>(),
);
Source

pub fn sort_by_key<K, F>(&mut self, f: F)
where F: FnMut(NodeRef<'_, T>) -> K, K: Ord,

Sort children by NodeRef’s key in ascending order using a key extraction function.

§Examples
use ego_tree::tree;

let mut tree = tree!("1a" => { "2b", "4c", "3d" });
tree.root_mut().sort_by_key(|a| a.value().split_at(1).0.parse::<i32>().unwrap());
assert_eq!(
    vec!["2b", "3d", "4c"],
    tree.root()
        .children()
        .map(|n| *n.value())
        .collect::<Vec<_>>(),
);

// Example for sort_by_id.
tree.root_mut().sort_by_key(|n| n.id());
assert_eq!(
    vec![&"2b", &"4c", &"3d"],
    tree.root()
        .children()
        .map(|n| n.value())
        .collect::<Vec<_>>(),
);
Source§

impl<'a, T: 'a> NodeMut<'a, T>

Source

pub fn id(&self) -> NodeId

Returns the ID of this node.

Source

pub fn tree(&mut self) -> &mut Tree<T>

Returns the tree owning this node.

Source

pub fn value(&mut self) -> &mut T

Returns the value of this node.

Source

pub fn parent(&mut self) -> Option<NodeMut<'_, T>>

Returns the parent of this node.

Source

pub fn into_parent(self) -> Result<Self, Self>

Returns the parent of this node.

Returns Ok(parent) if possible and Err(self) otherwise so the caller can recover the current position.

Source

pub fn prev_sibling(&mut self) -> Option<NodeMut<'_, T>>

Returns the previous sibling of this node.

Source

pub fn into_prev_sibling(self) -> Result<Self, Self>

Returns the previous sibling of this node.

Returns Ok(prev_sibling) if possible and Err(self) otherwise so the caller can recover the current position.

Source

pub fn next_sibling(&mut self) -> Option<NodeMut<'_, T>>

Returns the next sibling of this node.

Source

pub fn into_next_sibling(self) -> Result<Self, Self>

Returns the next sibling of this node.

Returns Ok(next_sibling) if possible and Err(self) otherwise so the caller can recover the current position.

Source

pub fn first_child(&mut self) -> Option<NodeMut<'_, T>>

Returns the first child of this node.

Source

pub fn into_first_child(self) -> Result<Self, Self>

Returns the first child of this node.

Returns Ok(first_child) if possible and Err(self) otherwise so the caller can recover the current position.

Source

pub fn last_child(&mut self) -> Option<NodeMut<'_, T>>

Returns the last child of this node.

Source

pub fn into_last_child(self) -> Result<Self, Self>

Returns the last child of this node.

Returns Ok(last_child) if possible and Err(self) otherwise so the caller can recover the current position.

Source

pub fn has_siblings(&self) -> bool

Returns true if this node has siblings.

Source

pub fn has_children(&self) -> bool

Returns true if this node has children.

Source

pub fn append(&mut self, value: T) -> NodeMut<'_, T>

Appends a new child to this node.

Source

pub fn prepend(&mut self, value: T) -> NodeMut<'_, T>

Prepends a new child to this node.

Source

pub fn append_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>

Appends a subtree, return the root of the merged subtree.

Source

pub fn prepend_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>

Prepends a subtree, return the root of the merged subtree.

Source

pub fn insert_before(&mut self, value: T) -> NodeMut<'_, T>

Inserts a new sibling before this node.

§Panics

Panics if this node is an orphan.

Source

pub fn insert_after(&mut self, value: T) -> NodeMut<'_, T>

Inserts a new sibling after this node.

§Panics

Panics if this node is an orphan.

Source

pub fn detach(&mut self)

Detaches this node from its parent.

Source

pub fn append_id(&mut self, new_child_id: NodeId) -> NodeMut<'_, T>

Appends a child to this node.

§Panics

Panics if new_child_id is not valid.

Source

pub fn prepend_id(&mut self, new_child_id: NodeId) -> NodeMut<'_, T>

Prepends a child to this node.

§Panics

Panics if new_child_id is not valid.

Source

pub fn insert_id_before(&mut self, new_sibling_id: NodeId) -> NodeMut<'_, T>

Inserts a sibling before this node.

§Panics
  • Panics if new_sibling_id is not valid.
  • Panics if this node is an orphan.
Source

pub fn insert_id_after(&mut self, new_sibling_id: NodeId) -> NodeMut<'_, T>

Inserts a sibling after this node.

§Panics
  • Panics if new_sibling_id is not valid.
  • Panics if this node is an orphan.
Source

pub fn reparent_from_id_append(&mut self, from_id: NodeId)

Reparents the children of a node, appending them to this node.

§Panics

Panics if from_id is not valid.

Source

pub fn reparent_from_id_prepend(&mut self, from_id: NodeId)

Reparents the children of a node, prepending them to this node.

§Panics

Panics if from_id is not valid.

Trait Implementations§

Source§

impl<'a, T: Debug + 'a> Debug for NodeMut<'a, T>

Source§

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

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

impl<'a, T: 'a> From<NodeMut<'a, T>> for NodeRef<'a, T>

Source§

fn from(node: NodeMut<'a, T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, T> Freeze for NodeMut<'a, T>

§

impl<'a, T> RefUnwindSafe for NodeMut<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for NodeMut<'a, T>
where T: Send,

§

impl<'a, T> Sync for NodeMut<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for NodeMut<'a, T>

§

impl<'a, T> !UnwindSafe for NodeMut<'a, 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> 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.