Skip to content

Commit

Permalink
Address review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 committed Jan 12, 2020
1 parent 091ba6d commit d2c509a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ pub struct Cursor<'a, T: 'a> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
impl<T: fmt::Debug> fmt::Debug for Cursor<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("Cursor").field(&self.list).field(&self.index).finish()
f.debug_tuple("Cursor").field(&self.list).field(&self.index()).finish()
}
}

Expand All @@ -1158,11 +1158,21 @@ pub struct CursorMut<'a, T: 'a> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
impl<T: fmt::Debug> fmt::Debug for CursorMut<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CursorMut").field(&self.list).field(&self.index).finish()
f.debug_tuple("CursorMut").field(&self.list).field(&self.index()).finish()
}
}

impl<'a, T> Cursor<'a, T> {
/// Returns the cursor position index within the `LinkedList`.
///
/// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn index(&self) -> Option<usize> {
let _ = self.current?;
Some(self.index)
}

/// Moves the cursor to the next element of the `LinkedList`.
///
/// If the cursor is pointing to the "ghost" non-element then this will move it to
Expand Down Expand Up @@ -1250,6 +1260,16 @@ impl<'a, T> Cursor<'a, T> {
}

impl<'a, T> CursorMut<'a, T> {
/// Returns the cursor position index within the `LinkedList`.
///
/// This returns `None` if the cursor is currently pointing to the
/// "ghost" non-element.
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn index(&self) -> Option<usize> {
let _ = self.current?;
Some(self.index)
}

/// Moves the cursor to the next element of the `LinkedList`.
///
/// If the cursor is pointing to the "ghost" non-element then this will move it to
Expand Down Expand Up @@ -1456,6 +1476,7 @@ impl<'a, T> CursorMut<'a, T> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn split_after(self) -> LinkedList<T> {
let split_off_idx = if self.index == self.list.len { 0 } else { self.index + 1 };
// no need to update `self.index` because the cursor is consumed.
unsafe { self.list.split_off_after_node(self.current, split_off_idx) }
}

Expand All @@ -1468,6 +1489,7 @@ impl<'a, T> CursorMut<'a, T> {
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn split_before(self) -> LinkedList<T> {
let split_off_idx = self.index;
// no need to update `self.index` because the cursor is consumed.
unsafe { self.list.split_off_before_node(self.current, split_off_idx) }
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/liballoc/collections/linked_list/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,36 +313,45 @@ fn test_cursor_move_peek() {
assert_eq!(cursor.current(), Some(&1));
assert_eq!(cursor.peek_next(), Some(&2));
assert_eq!(cursor.peek_prev(), None);
assert_eq!(cursor.index(), Some(0));
cursor.move_prev();
assert_eq!(cursor.current(), None);
assert_eq!(cursor.peek_next(), Some(&1));
assert_eq!(cursor.peek_prev(), Some(&6));
assert_eq!(cursor.index(), None);
cursor.move_next();
cursor.move_next();
assert_eq!(cursor.current(), Some(&2));
assert_eq!(cursor.peek_next(), Some(&3));
assert_eq!(cursor.peek_prev(), Some(&1));
assert_eq!(cursor.index(), Some(1));

let mut m: LinkedList<u32> = LinkedList::new();
m.extend(&[1, 2, 3, 4, 5, 6]);
let mut cursor = m.cursor_mut();
assert_eq!(cursor.current(), Some(&mut 1));
assert_eq!(cursor.peek_next(), Some(&mut 2));
assert_eq!(cursor.peek_prev(), None);
assert_eq!(cursor.index(), Some(0));
cursor.move_prev();
assert_eq!(cursor.current(), None);
assert_eq!(cursor.peek_next(), Some(&mut 1));
assert_eq!(cursor.peek_prev(), Some(&mut 6));
assert_eq!(cursor.index(), None);
cursor.move_next();
cursor.move_next();
assert_eq!(cursor.current(), Some(&mut 2));
assert_eq!(cursor.peek_next(), Some(&mut 3));
assert_eq!(cursor.peek_prev(), Some(&mut 1));
assert_eq!(cursor.index(), Some(1));
let mut cursor2 = cursor.as_cursor();
assert_eq!(cursor2.current(), Some(&2));
assert_eq!(cursor2.index(), Some(1));
cursor2.move_next();
assert_eq!(cursor2.current(), Some(&3));
assert_eq!(cursor2.index(), Some(2));
assert_eq!(cursor.current(), Some(&mut 2));
assert_eq!(cursor.index(), Some(1));
}

#[test]
Expand Down

0 comments on commit d2c509a

Please sign in to comment.