diff options
| author | Ian Wahbe <ian@wahbe.com> | 2021-06-29 15:35:14 +0200 |
|---|---|---|
| committer | Ian Wahbe <ian@wahbe.com> | 2021-06-29 15:35:14 +0200 |
| commit | e77acf7d27f7690f6cdb18d2ce68a37cbabd884c (patch) | |
| tree | 522840ce0c2b74515cc12275a5b04a87b6bd04f5 | |
| parent | a981be75cc45d0640acf1c86fc17166e8bdf953e (diff) | |
| download | rust-e77acf7d27f7690f6cdb18d2ce68a37cbabd884c.tar.gz rust-e77acf7d27f7690f6cdb18d2ce68a37cbabd884c.zip | |
Add non-mutable methods to `Cursor`
| -rw-r--r-- | library/alloc/src/collections/linked_list.rs | 14 | ||||
| -rw-r--r-- | library/alloc/src/collections/linked_list/tests.rs | 3 |
2 files changed, 17 insertions, 0 deletions
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); |
