pub struct Cursor<'a, T> { /* private fields */ }Expand description
A cursor with immutable access to a PieList.
A Cursor provides read-only navigation through a PieList. Unlike an
iterator, a cursor can move back and forth freely.
The cursor has two special states:
- “Before Start”: Positioned before the first element.
- “After End”: Positioned after the last element.
§Example
use pie_core::{ElemPool, PieList};
let mut pool = ElemPool::new();
let mut list = PieList::new(&mut pool);
list.push_back(10, &mut pool).unwrap();
list.push_back(20, &mut pool).unwrap();
let mut cursor = list.cursor(&pool);
// Starts at first element (10)
assert_eq!(cursor.peek(&pool), Some(&10));
// Move to next
cursor.move_next(&pool);
assert_eq!(cursor.peek(&pool), Some(&20));
// Move back
cursor.move_prev(&pool);
assert_eq!(cursor.peek(&pool), Some(&10));
// Clean up
list.clear(&mut pool);Implementations§
Source§impl<'a, T> Cursor<'a, T>
impl<'a, T> Cursor<'a, T>
Sourcepub fn index(&self) -> Option<usize>
pub fn index(&self) -> Option<usize>
Returns the logical index of the cursor’s current position.
Returns None if the cursor is pointing to the sentinel (i.e., is
“Before Start” or “After End”).
§Example
let mut cursor = list.cursor(&pool); // Starts at index 0
assert_eq!(cursor.index(), Some(0));
cursor.move_next(&pool); // Moves to After End
assert_eq!(cursor.index(), None);Sourcepub fn peek<'p>(&self, pool: &'p ElemPool<T>) -> Option<&'p T>
pub fn peek<'p>(&self, pool: &'p ElemPool<T>) -> Option<&'p T>
Provides a reference to the element at the cursor’s current position.
Returns None if the cursor is “Before Start” or “After End”.
§Example
let mut cursor = list.cursor(&pool);
assert_eq!(cursor.peek(&pool), Some(&10));Sourcepub fn move_next(&mut self, pool: &ElemPool<T>)
pub fn move_next(&mut self, pool: &ElemPool<T>)
Moves the cursor to the next element.
If the cursor is “Before Start”, it moves to the first element (or “After End” if empty). If the cursor is at the last element, it moves to “After End”. If the cursor is “After End”, it stays there (no-op).
§Example
let mut cursor = list.cursor(&pool); // At 0
cursor.move_next(&pool); // Now After End
assert!(cursor.peek(&pool).is_none());Sourcepub fn move_prev(&mut self, pool: &ElemPool<T>)
pub fn move_prev(&mut self, pool: &ElemPool<T>)
Moves the cursor to the previous element.
If the cursor is “After End”, it moves to the last element. If the cursor is at the first element, it moves to “Before Start”. If the cursor is “Before Start”, it stays there (no-op).
§Example
let mut cursor = list.cursor(&pool); // At 0
cursor.move_prev(&pool); // Now Before Start
assert!(cursor.peek(&pool).is_none());Sourcepub fn move_to_front(&mut self, pool: &ElemPool<T>)
pub fn move_to_front(&mut self, pool: &ElemPool<T>)
Moves the cursor to the first element of the list.
If the list is empty, the cursor ends up in the “After End” state (index = 0, len = 0).
§Example
let mut cursor = list.cursor(&pool);
cursor.move_to_back(&pool);
cursor.move_to_front(&pool);
assert_eq!(cursor.peek(&pool), Some(&1));Sourcepub fn move_to_back(&mut self, pool: &ElemPool<T>)
pub fn move_to_back(&mut self, pool: &ElemPool<T>)
Moves the cursor to the last element of the list.
If the list is empty, the cursor ends up in the “After End” state.
§Example
let mut cursor = list.cursor(&pool);
cursor.move_to_back(&pool);
assert_eq!(cursor.peek(&pool), Some(&2));