CursorMut

Struct CursorMut 

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

Source

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

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

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

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

pub fn navigate_to(&mut self, index: usize)

Set ‘current’ to ‘current’.childs[index], therefore navigating to this child

§Examples
let mut tree = Tree::from_element(0);
tree.push(1);
let mut cursor = tree.cursor_mut();
cursor.navigate_to(0);
assert_eq!(cursor.peek(), &1);
§Panics

This method will panic if index >= self.childs_len

Source

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

Source

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

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

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

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

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

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

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

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

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

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

Auto Trait Implementations§

§

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

§

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

§

impl<'a, T> !Send for CursorMut<'a, T>

§

impl<'a, T> !Sync for CursorMut<'a, T>

§

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

§

impl<'a, T> UnwindSafe for CursorMut<'a, T>
where T: RefUnwindSafe,

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.