Skip to content

Commit 06b9a73

Browse files
committed
Update APIs according to RFC change suggestions.
1 parent d2c509a commit 06b9a73

File tree

2 files changed

+85
-18
lines changed

2 files changed

+85
-18
lines changed

src/liballoc/collections/linked_list.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -509,20 +509,42 @@ impl<T> LinkedList<T> {
509509
IterMut { head: self.head, tail: self.tail, len: self.len, list: self }
510510
}
511511

512-
/// Provides a cursor.
512+
/// Provides a cursor at the front element.
513+
///
514+
/// The cursor is pointing to the "ghost" non-element if the list is empty.
513515
#[inline]
514516
#[unstable(feature = "linked_list_cursors", issue = "58533")]
515-
pub fn cursor(&self) -> Cursor<'_, T> {
517+
pub fn cursor_front(&self) -> Cursor<'_, T> {
516518
Cursor { index: 0, current: self.head, list: self }
517519
}
518520

519-
/// Provides a cursor with editing operations.
521+
/// Provides a cursor with editing operations at the front element.
522+
///
523+
/// The cursor is pointing to the "ghost" non-element if the list is empty.
520524
#[inline]
521525
#[unstable(feature = "linked_list_cursors", issue = "58533")]
522-
pub fn cursor_mut(&mut self) -> CursorMut<'_, T> {
526+
pub fn cursor_front_mut(&mut self) -> CursorMut<'_, T> {
523527
CursorMut { index: 0, current: self.head, list: self }
524528
}
525529

530+
/// Provides a cursor at the back element.
531+
///
532+
/// The cursor is pointing to the "ghost" non-element if the list is empty.
533+
#[inline]
534+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
535+
pub fn cursor_back(&self) -> Cursor<'_, T> {
536+
Cursor { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
537+
}
538+
539+
/// Provides a cursor with editing operations at the back element.
540+
///
541+
/// The cursor is pointing to the "ghost" non-element if the list is empty.
542+
#[inline]
543+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
544+
pub fn cursor_back_mut(&mut self) -> CursorMut<'_, T> {
545+
CursorMut { index: self.len.checked_sub(1).unwrap_or(0), current: self.tail, list: self }
546+
}
547+
526548
/// Returns `true` if the `LinkedList` is empty.
527549
///
528550
/// This operation should compute in O(1) time.
@@ -1146,8 +1168,6 @@ impl<T: fmt::Debug> fmt::Debug for Cursor<'_, T> {
11461168
/// Cursors always rest between two elements in the list, and index in a logically circular way.
11471169
/// To accommodate this, there is a "ghost" non-element that yields `None` between the head and
11481170
/// tail of the list.
1149-
///
1150-
/// When created, cursors start at the front of the list, or the "ghost" non-element if the list is empty.
11511171
#[unstable(feature = "linked_list_cursors", issue = "58533")]
11521172
pub struct CursorMut<'a, T: 'a> {
11531173
index: usize,
@@ -1474,9 +1494,12 @@ impl<'a, T> CursorMut<'a, T> {
14741494
/// If the cursor is pointing at the "ghost" non-element then the entire contents
14751495
/// of the `LinkedList` are moved.
14761496
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1477-
pub fn split_after(self) -> LinkedList<T> {
1497+
pub fn split_after(&mut self) -> LinkedList<T> {
14781498
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.
1499+
if self.index == self.list.len {
1500+
// The "ghost" non-element's index has changed to 0.
1501+
self.index = 0;
1502+
}
14801503
unsafe { self.list.split_off_after_node(self.current, split_off_idx) }
14811504
}
14821505

@@ -1487,9 +1510,9 @@ impl<'a, T> CursorMut<'a, T> {
14871510
/// If the cursor is pointing at the "ghost" non-element then the entire contents
14881511
/// of the `LinkedList` are moved.
14891512
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1490-
pub fn split_before(self) -> LinkedList<T> {
1513+
pub fn split_before(&mut self) -> LinkedList<T> {
14911514
let split_off_idx = self.index;
1492-
// no need to update `self.index` because the cursor is consumed.
1515+
self.index = 0;
14931516
unsafe { self.list.split_off_before_node(self.current, split_off_idx) }
14941517
}
14951518
}

src/liballoc/collections/linked_list/tests.rs

+52-8
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ fn drain_to_empty_test() {
309309
fn test_cursor_move_peek() {
310310
let mut m: LinkedList<u32> = LinkedList::new();
311311
m.extend(&[1, 2, 3, 4, 5, 6]);
312-
let mut cursor = m.cursor();
312+
let mut cursor = m.cursor_front();
313313
assert_eq!(cursor.current(), Some(&1));
314314
assert_eq!(cursor.peek_next(), Some(&2));
315315
assert_eq!(cursor.peek_prev(), None);
@@ -326,9 +326,26 @@ fn test_cursor_move_peek() {
326326
assert_eq!(cursor.peek_prev(), Some(&1));
327327
assert_eq!(cursor.index(), Some(1));
328328

329+
let mut cursor = m.cursor_back();
330+
assert_eq!(cursor.current(), Some(&6));
331+
assert_eq!(cursor.peek_next(), None);
332+
assert_eq!(cursor.peek_prev(), Some(&5));
333+
assert_eq!(cursor.index(), Some(5));
334+
cursor.move_next();
335+
assert_eq!(cursor.current(), None);
336+
assert_eq!(cursor.peek_next(), Some(&1));
337+
assert_eq!(cursor.peek_prev(), Some(&6));
338+
assert_eq!(cursor.index(), None);
339+
cursor.move_prev();
340+
cursor.move_prev();
341+
assert_eq!(cursor.current(), Some(&5));
342+
assert_eq!(cursor.peek_next(), Some(&6));
343+
assert_eq!(cursor.peek_prev(), Some(&4));
344+
assert_eq!(cursor.index(), Some(4));
345+
329346
let mut m: LinkedList<u32> = LinkedList::new();
330347
m.extend(&[1, 2, 3, 4, 5, 6]);
331-
let mut cursor = m.cursor_mut();
348+
let mut cursor = m.cursor_front_mut();
332349
assert_eq!(cursor.current(), Some(&mut 1));
333350
assert_eq!(cursor.peek_next(), Some(&mut 2));
334351
assert_eq!(cursor.peek_prev(), None);
@@ -352,24 +369,51 @@ fn test_cursor_move_peek() {
352369
assert_eq!(cursor2.index(), Some(2));
353370
assert_eq!(cursor.current(), Some(&mut 2));
354371
assert_eq!(cursor.index(), Some(1));
372+
373+
let mut m: LinkedList<u32> = LinkedList::new();
374+
m.extend(&[1, 2, 3, 4, 5, 6]);
375+
let mut cursor = m.cursor_back_mut();
376+
assert_eq!(cursor.current(), Some(&mut 6));
377+
assert_eq!(cursor.peek_next(), None);
378+
assert_eq!(cursor.peek_prev(), Some(&mut 5));
379+
assert_eq!(cursor.index(), Some(5));
380+
cursor.move_next();
381+
assert_eq!(cursor.current(), None);
382+
assert_eq!(cursor.peek_next(), Some(&mut 1));
383+
assert_eq!(cursor.peek_prev(), Some(&mut 6));
384+
assert_eq!(cursor.index(), None);
385+
cursor.move_prev();
386+
cursor.move_prev();
387+
assert_eq!(cursor.current(), Some(&mut 5));
388+
assert_eq!(cursor.peek_next(), Some(&mut 6));
389+
assert_eq!(cursor.peek_prev(), Some(&mut 4));
390+
assert_eq!(cursor.index(), Some(4));
391+
let mut cursor2 = cursor.as_cursor();
392+
assert_eq!(cursor2.current(), Some(&5));
393+
assert_eq!(cursor2.index(), Some(4));
394+
cursor2.move_prev();
395+
assert_eq!(cursor2.current(), Some(&4));
396+
assert_eq!(cursor2.index(), Some(3));
397+
assert_eq!(cursor.current(), Some(&mut 5));
398+
assert_eq!(cursor.index(), Some(4));
355399
}
356400

357401
#[test]
358402
fn test_cursor_mut_insert() {
359403
let mut m: LinkedList<u32> = LinkedList::new();
360404
m.extend(&[1, 2, 3, 4, 5, 6]);
361-
let mut cursor = m.cursor_mut();
405+
let mut cursor = m.cursor_front_mut();
362406
cursor.insert_before(7);
363407
cursor.insert_after(8);
364408
check_links(&m);
365409
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[7, 1, 8, 2, 3, 4, 5, 6]);
366-
let mut cursor = m.cursor_mut();
410+
let mut cursor = m.cursor_front_mut();
367411
cursor.move_prev();
368412
cursor.insert_before(9);
369413
cursor.insert_after(10);
370414
check_links(&m);
371415
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[10, 7, 1, 8, 2, 3, 4, 5, 6, 9]);
372-
let mut cursor = m.cursor_mut();
416+
let mut cursor = m.cursor_front_mut();
373417
cursor.move_prev();
374418
assert_eq!(cursor.remove_current(), None);
375419
cursor.move_next();
@@ -383,7 +427,7 @@ fn test_cursor_mut_insert() {
383427
assert_eq!(cursor.remove_current(), Some(10));
384428
check_links(&m);
385429
assert_eq!(m.iter().cloned().collect::<Vec<_>>(), &[1, 8, 2, 3, 4, 5, 6]);
386-
let mut cursor = m.cursor_mut();
430+
let mut cursor = m.cursor_front_mut();
387431
let mut p: LinkedList<u32> = LinkedList::new();
388432
p.extend(&[100, 101, 102, 103]);
389433
let mut q: LinkedList<u32> = LinkedList::new();
@@ -395,12 +439,12 @@ fn test_cursor_mut_insert() {
395439
m.iter().cloned().collect::<Vec<_>>(),
396440
&[200, 201, 202, 203, 1, 100, 101, 102, 103, 8, 2, 3, 4, 5, 6]
397441
);
398-
let mut cursor = m.cursor_mut();
442+
let mut cursor = m.cursor_front_mut();
399443
cursor.move_prev();
400444
let tmp = cursor.split_before();
401445
assert_eq!(m.into_iter().collect::<Vec<_>>(), &[]);
402446
m = tmp;
403-
let mut cursor = m.cursor_mut();
447+
let mut cursor = m.cursor_front_mut();
404448
cursor.move_next();
405449
cursor.move_next();
406450
cursor.move_next();

0 commit comments

Comments
 (0)