Skip to content

Commit d2c509a

Browse files
committed
Address review comments.
1 parent 091ba6d commit d2c509a

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/liballoc/collections/linked_list.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,7 +1132,7 @@ pub struct Cursor<'a, T: 'a> {
11321132
#[unstable(feature = "linked_list_cursors", issue = "58533")]
11331133
impl<T: fmt::Debug> fmt::Debug for Cursor<'_, T> {
11341134
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1135-
f.debug_tuple("Cursor").field(&self.list).field(&self.index).finish()
1135+
f.debug_tuple("Cursor").field(&self.list).field(&self.index()).finish()
11361136
}
11371137
}
11381138

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

11651165
impl<'a, T> Cursor<'a, T> {
1166+
/// Returns the cursor position index within the `LinkedList`.
1167+
///
1168+
/// This returns `None` if the cursor is currently pointing to the
1169+
/// "ghost" non-element.
1170+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1171+
pub fn index(&self) -> Option<usize> {
1172+
let _ = self.current?;
1173+
Some(self.index)
1174+
}
1175+
11661176
/// Moves the cursor to the next element of the `LinkedList`.
11671177
///
11681178
/// 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> {
12501260
}
12511261

12521262
impl<'a, T> CursorMut<'a, T> {
1263+
/// Returns the cursor position index within the `LinkedList`.
1264+
///
1265+
/// This returns `None` if the cursor is currently pointing to the
1266+
/// "ghost" non-element.
1267+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1268+
pub fn index(&self) -> Option<usize> {
1269+
let _ = self.current?;
1270+
Some(self.index)
1271+
}
1272+
12531273
/// Moves the cursor to the next element of the `LinkedList`.
12541274
///
12551275
/// 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> {
14561476
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14571477
pub fn split_after(self) -> LinkedList<T> {
14581478
let split_off_idx = if self.index == self.list.len { 0 } else { self.index + 1 };
1479+
// no need to update `self.index` because the cursor is consumed.
14591480
unsafe { self.list.split_off_after_node(self.current, split_off_idx) }
14601481
}
14611482

@@ -1468,6 +1489,7 @@ impl<'a, T> CursorMut<'a, T> {
14681489
#[unstable(feature = "linked_list_cursors", issue = "58533")]
14691490
pub fn split_before(self) -> LinkedList<T> {
14701491
let split_off_idx = self.index;
1492+
// no need to update `self.index` because the cursor is consumed.
14711493
unsafe { self.list.split_off_before_node(self.current, split_off_idx) }
14721494
}
14731495
}

src/liballoc/collections/linked_list/tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,36 +313,45 @@ fn test_cursor_move_peek() {
313313
assert_eq!(cursor.current(), Some(&1));
314314
assert_eq!(cursor.peek_next(), Some(&2));
315315
assert_eq!(cursor.peek_prev(), None);
316+
assert_eq!(cursor.index(), Some(0));
316317
cursor.move_prev();
317318
assert_eq!(cursor.current(), None);
318319
assert_eq!(cursor.peek_next(), Some(&1));
319320
assert_eq!(cursor.peek_prev(), Some(&6));
321+
assert_eq!(cursor.index(), None);
320322
cursor.move_next();
321323
cursor.move_next();
322324
assert_eq!(cursor.current(), Some(&2));
323325
assert_eq!(cursor.peek_next(), Some(&3));
324326
assert_eq!(cursor.peek_prev(), Some(&1));
327+
assert_eq!(cursor.index(), Some(1));
325328

326329
let mut m: LinkedList<u32> = LinkedList::new();
327330
m.extend(&[1, 2, 3, 4, 5, 6]);
328331
let mut cursor = m.cursor_mut();
329332
assert_eq!(cursor.current(), Some(&mut 1));
330333
assert_eq!(cursor.peek_next(), Some(&mut 2));
331334
assert_eq!(cursor.peek_prev(), None);
335+
assert_eq!(cursor.index(), Some(0));
332336
cursor.move_prev();
333337
assert_eq!(cursor.current(), None);
334338
assert_eq!(cursor.peek_next(), Some(&mut 1));
335339
assert_eq!(cursor.peek_prev(), Some(&mut 6));
340+
assert_eq!(cursor.index(), None);
336341
cursor.move_next();
337342
cursor.move_next();
338343
assert_eq!(cursor.current(), Some(&mut 2));
339344
assert_eq!(cursor.peek_next(), Some(&mut 3));
340345
assert_eq!(cursor.peek_prev(), Some(&mut 1));
346+
assert_eq!(cursor.index(), Some(1));
341347
let mut cursor2 = cursor.as_cursor();
342348
assert_eq!(cursor2.current(), Some(&2));
349+
assert_eq!(cursor2.index(), Some(1));
343350
cursor2.move_next();
344351
assert_eq!(cursor2.current(), Some(&3));
352+
assert_eq!(cursor2.index(), Some(2));
345353
assert_eq!(cursor.current(), Some(&mut 2));
354+
assert_eq!(cursor.index(), Some(1));
346355
}
347356

348357
#[test]

0 commit comments

Comments
 (0)