Skip to content

Commit

Permalink
feat(cordyceps): add List::cursor_back_mut (#227)
Browse files Browse the repository at this point in the history
This adds `List::cursor_back_mut` returning a mutable cursor at the back
of the list.

Closes #224

Signed-off-by: Eliza Weisman <[email protected]>
  • Loading branch information
hawkw committed Jun 18, 2022
1 parent b147738 commit e53e071
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
13 changes: 13 additions & 0 deletions cordyceps/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,19 @@ impl<T: Linked<Links<T>> + ?Sized> List<T> {
}
}

/// Returns a [`Cursor`] s+tarting at the last element.
///
/// The [`Cursor`] type can be used as a mutable [`Iterator`]. In addition,
/// however, it also permits modifying the *structure* of the list by
/// inserting or removing elements at the cursor's current position.
#[must_use]
pub fn cursor_back_mut(&mut self) -> Cursor<'_, T> {
Cursor {
curr: self.tail,
list: self,
}
}

/// Returns an iterator over the items in this list, by reference.
#[must_use]
pub fn iter(&self) -> Iter<'_, T> {
Expand Down
28 changes: 13 additions & 15 deletions cordyceps/src/list/tests/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,21 @@ fn move_peek() {
assert_eq!(val(cursor.peek_prev()), Some(1));
// assert_eq!(cursor.index(), Some(1));

// TODO(eliza): we don't have a `cursor_back` yet...

// let mut cursor = m.cursor_back();
// assert_eq!(val(cursor.current()), Some(&6));
// assert_eq!(val(cursor.peek_next()), None);
// assert_eq!(val(cursor.peek_prev()), Some(&5));
let mut cursor = list.cursor_back_mut();
assert_eq!(val(cursor.current()), Some(6));
assert_eq!(val(cursor.peek_next()), None);
assert_eq!(val(cursor.peek_prev()), Some(5));
// assert_eq!(cursor.index(), Some(5));
// cursor.move_next();
// assert_eq!(val(cursor.current()), None);
// assert_eq!(val(cursor.peek_next()), Some(&1));
// assert_eq!(val(cursor.peek_prev()), Some(&6));
cursor.move_next();
assert_eq!(val(cursor.current()), None);
assert_eq!(val(cursor.peek_next()), Some(1));
assert_eq!(val(cursor.peek_prev()), Some(6));
// assert_eq!(cursor.index(), None);
// cursor.move_prev();
// cursor.move_prev();
// assert_eq!(val(cursor.current()), Some(&5));
// assert_eq!(val(cursor.peek_next()), Some(&6));
// assert_eq!(val(cursor.peek_prev()), Some(&4));
cursor.move_prev();
cursor.move_prev();
assert_eq!(val(cursor.current()), Some(5));
assert_eq!(val(cursor.peek_next()), Some(6));
assert_eq!(val(cursor.peek_prev()), Some(4));
// assert_eq!(cursor.index(), Some(4));

// let mut m: LinkedList<u32> = LinkedList::new();
Expand Down

0 comments on commit e53e071

Please sign in to comment.