pub struct CursorMut<'a, T> { /* private fields */ }Expand description
Equivalent of mutable reference for crate::Tree
This structure is the same as Cursor, implements every methods that Cursor implements and only implements a few more methods such as CursorMut::peek_mut.
CursorMut are created by taking a mutable reference to the tree, meaning that they can be only one CursorMut alive at the same time, and that creating a CursorMut invalidates every other Cursor or CursorMut.
That behaviour makes CursorMut a bit useless, as it is the same as navigating ‘current’ in tree. The most useful behaviour is when you need a mutable exploration of the tree without navigating ‘current’, and so instead of navigating ‘current’ back to it’s original location after you exploration (which can be sometimes a bit complex), you can create a CursorMut, explores the tree with it, and then throw it away as soon as you finished the exploration.
§Examples
let mut tree = Tree::from_element(10);
tree.push_iter(vec![1, 2, 3]);
// Firstly, node that we need a mut on the cursor definition.
// This is because the signature of peek_mut(&mut self) -> &mut T
// Doing so enforce rust ownership system, but it makes cursor immutable while the reference is
// alive.
let mut cursor = tree.cursor_mut();
let ref_el = cursor.peek_mut();
assert_eq!(ref_el, &mut 10);
*ref_el = 15;
cursor.navigate_to(0);
// but now we can't use ref_el, because navigate_to needs to mutate cursor, and so invalidates
// all mutable refences taken from cursor.
// Not very pratical if we, for exemple, want to build an iterator on the whole tree from a
// CursorMut.Implementations§
Source§impl<'a, T> CursorMut<'a, T>
impl<'a, T> CursorMut<'a, T>
Sourcepub fn peek(&self) -> &'a T
pub fn peek(&self) -> &'a T
Peek at ‘current’, returning a reference to the element stored in ‘current’.
§Examples
let mut tree = Tree::from_element(10);
let cursor = tree.cursor_mut();
assert_eq!(cursor.peek(), &10);Sourcepub fn peek_mut(&mut self) -> &mut T
pub fn peek_mut(&mut self) -> &mut T
Peek at ‘current’, returning a mutable reference to the element stored in ‘current’.
§Examples
let mut tree = Tree::from_element(10);
let mut cursor = tree.cursor_mut();
assert_eq!(cursor.peek_mut(), &mut 10);Sourcepub fn peek_child(&self, index: usize) -> &'a T
pub fn peek_child(&self, index: usize) -> &'a T
Peek at ‘current’.childs[index], returning a reference to the element stored.
§Examples
let mut tree = Tree::from_element(10);
tree.push(5);
let cursor = tree.cursor_mut();
assert_eq!(cursor.peek_child(0), &5);Sourcepub fn peek_child_mut(&mut self, index: usize) -> &mut T
pub fn peek_child_mut(&mut self, index: usize) -> &mut T
Peek at ‘current’.childs[index], returning a mutable reference to the element stored.
§Examples
let mut tree = Tree::from_element(10);
tree.push(5);
let mut cursor = tree.cursor_mut();
assert_eq!(cursor.peek_child_mut(0), &mut 5);Sourcepub fn ascend(&mut self)
pub fn ascend(&mut self)
Set ‘current’ to ‘current’.father, therefore navigating up.
§Examples
let mut tree = Tree::from_element(0);
tree.push(1);
tree.navigate_to(0);
let mut cursor = tree.cursor_mut();
cursor.ascend();
assert_eq!(cursor.peek(), &0);§Panics
This method will panic if ‘current’ has no father i.e. if ‘current’.father.is_none()
Sourcepub fn has_father(&self) -> bool
pub fn has_father(&self) -> bool
Return true if ‘current’ has a father.
§Examples
let mut tree = Tree::from_element(0);
tree.push(1);
tree.navigate_to(0);
let mut cursor = tree.cursor_mut();
assert!(cursor.has_father());
cursor.ascend();
assert!(!cursor.has_father());Sourcepub fn childs_len(&self) -> usize
pub fn childs_len(&self) -> usize
Return the number of childrens of current.
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 1, 1, 1, 1]);
let cursor = tree.cursor_mut();
assert_eq!(cursor.childs_len(), 5);Sourcepub fn iter_childs(&self) -> ChildIterator<'a, T>
pub fn iter_childs(&self) -> ChildIterator<'a, T>
Return an Iterator over the elements stored in ‘current’.childs
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
let cursor = tree.cursor_mut();
assert_eq!(cursor.iter_childs().collect::<Vec<&i32>>(), vec![&1, &2, &3]);Sourcepub fn iter_childs_mut(&self) -> ChildIteratorMut<'a, T>
pub fn iter_childs_mut(&self) -> ChildIteratorMut<'a, T>
Return an Iterator over the elements stored in ‘current’.childs
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
let mut cursor = tree.cursor_mut();
assert_eq!(cursor.iter_childs_mut().collect::<Vec<&mut i32>>(), vec![&mut 1, &mut 2, &mut 3]);Sourcepub fn iter(&self) -> impl Iterator<Item = &'a T>
pub fn iter(&self) -> impl Iterator<Item = &'a T>
Iterate over references of element stored in the subtree rooted at ‘current’ in a depth-first way. This is done by creating a Vec and pushing every references into this Vec and then returning an iterator over this Vec. As it may not be very memory efficient, you might check CursorMut::lazyiter.
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
tree.navigate_to(1);
tree.push(4);
assert_eq!(tree.cursor_mut().iter().collect::<Vec<&i32>>(), vec![&2, &4]);Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &'a mut T>
Same as CursorMut::iter, but returns mutable reference instead
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
tree.navigate_to(1);
tree.push(4);
assert_eq!(tree.cursor_mut().iter_mut().collect::<Vec<&mut i32>>(), vec![&mut 2, &mut 4]);Sourcepub fn lazyiter(&self) -> LazyTreeIterator<'a, T>
pub fn lazyiter(&self) -> LazyTreeIterator<'a, T>
Iterate over the subtree rooted at ‘current’ in a lazy depth-first way, returning references to the elements stored in the subtree. Although it is lazy iteration, meaning it is less stressfull for memory, it is slower than CursorMut::iter, because the cursor that is used to move around the tree has to keep tracks of which branches it has already explored.
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
tree.navigate_to(1);
tree.push_iter(vec![9, 8]);
tree.ascend();
tree.navigate_to(0);
tree.push_iter(vec![9, 10]);
tree.navigate_to(0);
tree.push(15);
tree.go_to_root();
let mut cursor = tree.cursor_mut();
assert_eq!(
cursor.lazyiter().collect::<Vec<&i32>>(),
vec![&0, &1, &9, &15, &10, &2, &9, &8, &3]
);
cursor.navigate_to(1);
assert_eq!(cursor.lazyiter().collect::<Vec<&i32>>(), vec![&2, &9, &8]);Sourcepub fn lazyiter_mut(&mut self) -> LazyTreeIteratorMut<'a, T>
pub fn lazyiter_mut(&mut self) -> LazyTreeIteratorMut<'a, T>
Same as CursorMut::lazyiter but returns mutable references instead
§Examples
let mut tree = Tree::from_element(0);
tree.push_iter(vec![1, 2, 3]);
tree.navigate_to(1);
tree.push_iter(vec![9, 8]);
tree.ascend();
tree.navigate_to(0);
tree.push_iter(vec![9, 10]);
tree.navigate_to(0);
tree.push(15);
tree.go_to_root();
let mut cursor = tree.cursor_mut();
assert_eq!(
cursor.lazyiter_mut().collect::<Vec<&mut i32>>(),
vec![&mut 0, &mut 1, &mut 9, &mut 15, &mut 10, &mut 2, &mut 9, &mut 8, &mut 3]
);Sourcepub fn push(&mut self, el: T)
pub fn push(&mut self, el: T)
Push el to ‘current’.child as a new node in the tree.
§Examples
let mut tree = Tree::from_element(1);
let mut cursor = tree.cursor_mut();
cursor.push(2);
cursor.navigate_to(0);
assert_eq!(cursor.peek(), &2);
assert_eq!(tree.into_vec(), vec![1, 2]);Sourcepub fn push_iter<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
pub fn push_iter<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Convenient method to push the elements of an iterator into the tree. It’s litteraly : for el in iter.into_iter() { tree.push(el) }
§Examples
let mut tree = Tree::from_element(0);
let mut cursor = tree.cursor_mut();
cursor.push_iter(vec![1, 2, 3]);
cursor.navigate_to(2);
assert_eq!(cursor.peek(), &3);
assert_eq!(tree.into_vec(), vec![0, 1, 2, 3]);