about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCharles Lew <crlf0710@gmail.com>2020-01-12 18:01:24 +0800
committerCharles Lew <crlf0710@gmail.com>2020-01-12 18:34:35 +0800
commitd2c509a3c6b06ba02807bf94c00fad4c4a9262a5 (patch)
tree59288e3a32b45eb3f1bff8332cf41cd03a36edb3
parent091ba6daa0a0a528b5d9fc816529a9bb25503960 (diff)
downloadrust-d2c509a3c6b06ba02807bf94c00fad4c4a9262a5.tar.gz
rust-d2c509a3c6b06ba02807bf94c00fad4c4a9262a5.zip
Address review comments.
-rw-r--r--src/liballoc/collections/linked_list.rs26
-rw-r--r--src/liballoc/collections/linked_list/tests.rs9
2 files changed, 33 insertions, 2 deletions
diff --git a/src/liballoc/collections/linked_list.rs b/src/liballoc/collections/linked_list.rs
index 3524c6e9817..d7504b0b80b 100644
--- a/src/liballoc/collections/linked_list.rs
+++ b/src/liballoc/collections/linked_list.rs
@@ -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()
     }
 }
 
@@ -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
@@ -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
@@ -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) }
     }
 
@@ -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) }
     }
 }
diff --git a/src/liballoc/collections/linked_list/tests.rs b/src/liballoc/collections/linked_list/tests.rs
index 29eb1b4abd7..d223752c7f7 100644
--- a/src/liballoc/collections/linked_list/tests.rs
+++ b/src/liballoc/collections/linked_list/tests.rs
@@ -313,15 +313,18 @@ 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]);
@@ -329,20 +332,26 @@ fn test_cursor_move_peek() {
     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]