@@ -1247,14 +1247,14 @@ impl<'a, T> Cursor<'a, T> {
1247
1247
/// Provides a reference to the front element of the cursor's parent list,
1248
1248
/// or None if the list is empty.
1249
1249
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1250
- pub fn front ( & self ) -> Option < & T > {
1250
+ pub fn front ( & self ) -> Option < & ' a T > {
1251
1251
self . list . front ( )
1252
1252
}
1253
1253
1254
1254
/// Provides a reference to the back element of the cursor's parent list,
1255
1255
/// or None if the list is empty.
1256
1256
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
1257
- pub fn back ( & self ) -> Option < & T > {
1257
+ pub fn back ( & self ) -> Option < & ' a T > {
1258
1258
self . list . back ( )
1259
1259
}
1260
1260
}
@@ -1546,6 +1546,11 @@ impl<'a, T> CursorMut<'a, T> {
1546
1546
// memory of other nodes. This ensures that `self.current` remains
1547
1547
// valid.
1548
1548
self . list . push_back ( elt) ;
1549
+ if self . current ( ) . is_none ( ) {
1550
+ // The index of "ghost" is the length of the list, so we just need
1551
+ // to increment self.index to reflect the new length of the list.
1552
+ self . index += 1 ;
1553
+ }
1549
1554
}
1550
1555
1551
1556
/// Removes the first element from the cursor's parent list and returns it,
@@ -1565,21 +1570,20 @@ impl<'a, T> CursorMut<'a, T> {
1565
1570
// We can't point to the node that we pop. Copying the behavior of
1566
1571
// `remove_current`, we move on the the next node in the sequence.
1567
1572
// If the list is of length 1 then we end pointing to the "ghost"
1568
- // node, which is expected.
1573
+ // node at index 0 , which is expected.
1569
1574
if self . list . head == self . current {
1570
1575
self . move_next ( ) ;
1576
+ } else {
1577
+ self . index -= 1 ;
1571
1578
}
1572
- // We always need to change the index since `head` comes before any
1573
- // other element.
1574
- self . index . checked_sub ( 1 ) . unwrap_or ( 0 ) ;
1575
1579
self . list . pop_front ( )
1576
1580
}
1577
1581
}
1578
1582
1579
1583
/// Removes the last element from the cursor's parent list and returns it,
1580
1584
/// or None if the list is empty. The element the cursor points to remains
1581
1585
/// unchanged, unless it was pointing to the back element. In that case, it
1582
- /// points to the new back element.
1586
+ /// points to the "ghost" element.
1583
1587
///
1584
1588
/// This operation should compute in O(1) time.
1585
1589
#[ unstable( feature = "linked_list_cursors" , issue = "58533" ) ]
@@ -1588,10 +1592,13 @@ impl<'a, T> CursorMut<'a, T> {
1588
1592
None
1589
1593
} else {
1590
1594
if self . list . tail == self . current {
1591
- self . move_prev ( )
1595
+ // The index now reflects the length of the list. It was the
1596
+ // length of the list minus 1, but now the list is 1 smaller. No
1597
+ // change is needed for `index`.
1598
+ self . current = None ;
1599
+ } else if self . current . is_none ( ) {
1600
+ self . index = self . list . len - 1 ;
1592
1601
}
1593
- // We don't need to change the index since `current` points to a
1594
- // node before `tail`.
1595
1602
self . list . pop_back ( )
1596
1603
}
1597
1604
}
0 commit comments