Crate cycle_cursor

Source
Expand description

§Cycle Cursor

A cyclic bidirectional cursor implementation over generic iterators.

To begin, create an instance of the CycleCursor


// Cursor from [`Vec`]
let mut vec_cursor = CycleCursor::from(vec![1, 2, 3, 4]);
// Cursor from [`BTreeSet`]
let mut btree_cursor = CycleCursor::from(BTreeSet::from([2, 2, 4, 6, 8, 8]));

// Cyclically go to next element
vec_cursor.cycle_next();

// Cyclically go to previous element
vec_cursor.cycle_prev();
vec_cursor.cycle_prev();

// Get the current pointed element
assert_eq!(vec_cursor.get().unwrap(), &3);

// Cyclically peek an element without moving the cursor
assert_eq!(vec_cursor.peek(5).unwrap(), &4);
assert_eq!(vec_cursor.peek(-2).unwrap(), &1);
assert_eq!(vec_cursor.get().unwrap(), &3);

// Cyclically seek the cursor by signed offset
vec_cursor.seek(5);
assert_eq!(vec_cursor.get().unwrap(), &4);
vec_cursor.seek(-2);
assert_eq!(vec_cursor.get().unwrap(), &2);

Structs§

CycleCursor
Implements a cycling, seekable and peekable cursor over an iterable.