Skip to main content

Cursor

Struct Cursor 

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

  1. “Before Start”: Positioned before the first element.
  2. “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>

Source

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

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

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

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

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

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

Trait Implementations§

Source§

impl<'a, T: Debug> Debug for Cursor<'a, T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

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

§

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

§

impl<'a, T> Send for Cursor<'a, T>
where T: Send + Sync,

§

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

§

impl<'a, T> Unpin for Cursor<'a, T>
where T: Unpin,

§

impl<'a, T> UnsafeUnpin for Cursor<'a, T>

§

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