pub struct NodeMut<'a, T: 'a> { /* private fields */ }
Expand description
Node mutator.
Implementations§
Source§impl<'a, T: 'a> NodeMut<'a, T>
impl<'a, T: 'a> NodeMut<'a, T>
Sourcepub fn sort(&mut self)where
T: Ord,
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<_>>(),
);
Sourcepub fn sort_by<F>(&mut self, compare: F)
pub fn sort_by<F>(&mut self, compare: F)
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<_>>(),
);
Sourcepub fn sort_by_key<K, F>(&mut self, f: F)
pub fn sort_by_key<K, F>(&mut self, f: F)
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>
impl<'a, T: 'a> NodeMut<'a, T>
Sourcepub fn into_parent(self) -> Result<Self, Self>
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.
Sourcepub fn prev_sibling(&mut self) -> Option<NodeMut<'_, T>>
pub fn prev_sibling(&mut self) -> Option<NodeMut<'_, T>>
Returns the previous sibling of this node.
Sourcepub fn into_prev_sibling(self) -> Result<Self, Self>
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.
Sourcepub fn next_sibling(&mut self) -> Option<NodeMut<'_, T>>
pub fn next_sibling(&mut self) -> Option<NodeMut<'_, T>>
Returns the next sibling of this node.
Sourcepub fn into_next_sibling(self) -> Result<Self, Self>
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.
Sourcepub fn first_child(&mut self) -> Option<NodeMut<'_, T>>
pub fn first_child(&mut self) -> Option<NodeMut<'_, T>>
Returns the first child of this node.
Sourcepub fn into_first_child(self) -> Result<Self, Self>
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.
Sourcepub fn last_child(&mut self) -> Option<NodeMut<'_, T>>
pub fn last_child(&mut self) -> Option<NodeMut<'_, T>>
Returns the last child of this node.
Sourcepub fn into_last_child(self) -> Result<Self, Self>
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.
Sourcepub fn has_siblings(&self) -> bool
pub fn has_siblings(&self) -> bool
Returns true if this node has siblings.
Sourcepub fn has_children(&self) -> bool
pub fn has_children(&self) -> bool
Returns true if this node has children.
Sourcepub fn append_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>
pub fn append_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>
Appends a subtree, return the root of the merged subtree.
Sourcepub fn prepend_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>
pub fn prepend_subtree(&mut self, subtree: Tree<T>) -> NodeMut<'_, T>
Prepends a subtree, return the root of the merged subtree.
Sourcepub fn insert_before(&mut self, value: T) -> NodeMut<'_, T>
pub fn insert_before(&mut self, value: T) -> NodeMut<'_, T>
Sourcepub fn insert_after(&mut self, value: T) -> NodeMut<'_, T>
pub fn insert_after(&mut self, value: T) -> NodeMut<'_, T>
Sourcepub fn prepend_id(&mut self, new_child_id: NodeId) -> NodeMut<'_, T>
pub fn prepend_id(&mut self, new_child_id: NodeId) -> NodeMut<'_, T>
Sourcepub fn insert_id_before(&mut self, new_sibling_id: NodeId) -> NodeMut<'_, T>
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.
Sourcepub fn insert_id_after(&mut self, new_sibling_id: NodeId) -> NodeMut<'_, T>
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.
Sourcepub fn reparent_from_id_append(&mut self, from_id: NodeId)
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.
Sourcepub fn reparent_from_id_prepend(&mut self, from_id: NodeId)
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.