Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2206c6b

Browse files
authoredJul 7, 2024
Rollup merge of rust-lang#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.
2 parents 41bcc36 + bba2200 commit 2206c6b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
 

‎alloc/src/collections/linked_list.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,14 @@ impl<'a, T, A: Allocator> Cursor<'a, T, A> {
14951495
pub fn back(&self) -> Option<&'a T> {
14961496
self.list.back()
14971497
}
1498+
1499+
/// Provides a reference to the cursor's parent list.
1500+
#[must_use]
1501+
#[inline(always)]
1502+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1503+
pub fn as_list(&self) -> &'a LinkedList<T, A> {
1504+
self.list
1505+
}
14981506
}
14991507

15001508
impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
@@ -1605,6 +1613,18 @@ impl<'a, T, A: Allocator> CursorMut<'a, T, A> {
16051613
pub fn as_cursor(&self) -> Cursor<'_, T, A> {
16061614
Cursor { list: self.list, current: self.current, index: self.index }
16071615
}
1616+
1617+
/// Provides a read-only reference to the cursor's parent list.
1618+
///
1619+
/// The lifetime of the returned reference is bound to that of the
1620+
/// `CursorMut`, which means it cannot outlive the `CursorMut` and that the
1621+
/// `CursorMut` is frozen for the lifetime of the reference.
1622+
#[must_use]
1623+
#[inline(always)]
1624+
#[unstable(feature = "linked_list_cursors", issue = "58533")]
1625+
pub fn as_list(&self) -> &LinkedList<T, A> {
1626+
self.list
1627+
}
16081628
}
16091629

16101630
// Now the list editing operations

0 commit comments

Comments
 (0)
Failed to load comments.