From e77acf7d27f7690f6cdb18d2ce68a37cbabd884c Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Tue, 29 Jun 2021 15:35:14 +0200 Subject: [PATCH] Add non-mutable methods to `Cursor` --- library/alloc/src/collections/linked_list.rs | 14 ++++++++++++++ library/alloc/src/collections/linked_list/tests.rs | 3 +++ 2 files changed, 17 insertions(+) diff --git a/library/alloc/src/collections/linked_list.rs b/library/alloc/src/collections/linked_list.rs index a21eedf02f1..4eecf5703a4 100644 --- a/library/alloc/src/collections/linked_list.rs +++ b/library/alloc/src/collections/linked_list.rs @@ -1243,6 +1243,20 @@ impl<'a, T> Cursor<'a, T> { prev.map(|prev| &(*prev.as_ptr()).element) } } + + /// Provides a reference to the front element of the cursor's parent list, + /// or None if the list is empty. + #[unstable(feature = "linked_list_cursors", issue = "58533")] + pub fn front(&self) -> Option<&T> { + self.list.front() + } + + /// Provides a reference to the back element of the cursor's parent list, + /// or None if the list is empty. + #[unstable(feature = "linked_list_cursors", issue = "58533")] + pub fn back(&self) -> Option<&T> { + self.list.back() + } } impl<'a, T> CursorMut<'a, T> { diff --git a/library/alloc/src/collections/linked_list/tests.rs b/library/alloc/src/collections/linked_list/tests.rs index 429685260fd..45349a7df12 100644 --- a/library/alloc/src/collections/linked_list/tests.rs +++ b/library/alloc/src/collections/linked_list/tests.rs @@ -456,6 +456,9 @@ fn test_cursor_pop_front_back() { c.move_prev(); c.move_prev(); assert_eq!(c.pop_back(), Some(6)); + let c = c.as_cursor(); + assert_eq!(c.front(), Some(&2)); + assert_eq!(c.back(), Some(&5)); drop(c); assert_eq!(ll, (2..6).collect()); check_links(&ll);