Rollup merge of #127189 - GrigorenkoPV:linkedlist-cursor-list, r=Nilstrieb

LinkedList's Cursor: method to get a ref to the cursor's list

We're already providing `.back()` & `.front()`, for which we hold onto a reference to the parent list, so why not share it? Useful for when you got `LinkedList` -> `CursorMut` -> `Cursor` and cannot take another ref to the list, even though you should be able to. This seems to be completely safe & sound.

The name is, of course, bikesheddable.
This commit is contained in:
Matthias Krüger 2024-07-07 14:21:59 +02:00 committed by GitHub
commit 94d07befe7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1495,6 +1495,14 @@ pub fn front(&self) -> Option<&'a T> {
pub fn back(&self) -> Option<&'a T> {
self.list.back()
}
/// Provides a reference to the cursor's parent list.
#[must_use]
#[inline(always)]
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn as_list(&self) -> &'a LinkedList<T, A> {
self.list
}
}
impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
@ -1605,6 +1613,18 @@ pub fn peek_prev(&mut self) -> Option<&mut T> {
pub fn as_cursor(&self) -> Cursor<'_, T, A> {
Cursor { list: self.list, current: self.current, index: self.index }
}
/// Provides a read-only reference to the cursor's parent list.
///
/// The lifetime of the returned reference is bound to that of the
/// `CursorMut`, which means it cannot outlive the `CursorMut` and that the
/// `CursorMut` is frozen for the lifetime of the reference.
#[must_use]
#[inline(always)]
#[unstable(feature = "linked_list_cursors", issue = "58533")]
pub fn as_list(&self) -> &LinkedList<T, A> {
self.list
}
}
// Now the list editing operations