Skip to content

Commit 772fb67

Browse files
Rename a few more methods in Linked List Cursor RFC
These are just the names of the initially implementation.
1 parent 41a81ea commit 772fb67

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

text/2570-linked-list-cursors.md

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ list many times. With the cursor interface, one can do the following:
6363
fn remove_replace<T, P, F>(list: &mut LinkedList<T>, p: P, f: F)
6464
where P: Fn(&T) -> bool, F: Fn(T) -> T
6565
{
66-
let mut cursor = list.cursor_mut();
66+
let mut cursor = list.cursor_front_mut();
6767
// move to the first element, if it exists
6868
loop {
6969
let should_replace = match cursor.peek_next() {
@@ -101,7 +101,7 @@ This can be changed almost verbatim to `CursorMut`:
101101
``` rust
102102
fn main() {
103103
let mut list: LinkedList<_> = (0..10).collect();
104-
let mut cursor = list.cursor_mut() {
104+
let mut cursor = list.cursor_front_mut() {
105105
while let Some(x) = cursor.peek_next() {
106106
if x >= 5 {
107107
break;
@@ -122,17 +122,26 @@ One gets a cursor the exact same way as one would get an iterator. The
122122
returned cursor would point to the "empty" element, i.e. if you got an element
123123
and called `current` you would receive `None`.
124124
``` rust
125-
// Provides a cursor to the first element of the list
126-
pub fn cursor(&self) -> Cursor<T>;
125+
/// Provides a cursor to the first element of the list.
126+
pub fn cursor_front(&self) -> Cursor<T>;
127127

128-
/// Provides a cursor with mutable references and access to the list
129-
pub fn cursor_mut(&mut self) -> CursorMut<T>;
128+
/// Provides a mutable cursor to the first element of the list.
129+
pub fn cursor_front_mut(&mut self) -> CursorMut<T>;
130+
131+
/// Provides a cursor to the last element of the list.
132+
pub fn cursor_back(&self) -> Cursor<T>;
133+
134+
/// Provides a mutable cursor to the last element of the list.
135+
pub fn cursor_back_mut(&mut self) -> CursorMut<T>;
130136
```
131137

132138
These would provide the following interface:
133139

134140
``` rust
135141
impl<'list, T> Cursor<'list, T> {
142+
/// Returns the cursor position index within the `LinkedList`.
143+
pub fn index(&self) -> Option<usize>;
144+
136145
/// Move to the subsequent element of the list if it exists or the empty
137146
/// element
138147
pub fn move_next(&mut self);
@@ -148,6 +157,9 @@ impl<'list, T> Cursor<'list, T> {
148157
}
149158

150159
impl<'list T> CursorMut<'list, T> {
160+
/// Returns the cursor position index within the `LinkedList`.
161+
pub fn index(&self) -> Option<usize>;
162+
151163
/// Move to the subsequent element of the list if it exists or the empty
152164
/// element
153165
pub fn move_next(&mut self);
@@ -182,11 +194,9 @@ impl<'list T> CursorMut<'list, T> {
182194

183195
/// Split the list in two after the current element
184196
/// The returned list consists of all elements following the current one.
185-
// note: consuming the cursor is not necessary here, but it makes sense
186-
// given the interface
187-
pub fn split_after(self) -> LinkedList<T>;
197+
pub fn split_after(&mut self) -> LinkedList<T>;
188198
/// Split the list in two before the current element
189-
pub fn split_before(self) -> LinkedList<T>;
199+
pub fn split_before(&mut self) -> LinkedList<T>;
190200
}
191201
```
192202
One should closely consider the lifetimes in this interface. Both `Cursor` and
@@ -195,11 +205,11 @@ the annotation of `'list`.
195205

196206
The lifetime elision for their constructors is correct as
197207
```rust
198-
pub fn cursor(&self) -> Cursor<T>
208+
pub fn cursor_front(&self) -> Cursor<T>
199209
```
200210
becomes
201211
```rust
202-
pub fn cursor<'list>(&'list self) -> Cursor<'list, T>
212+
pub fn cursor_front<'list>(&'list self) -> Cursor<'list, T>
203213
```
204214
which is what we would expect. (the same goes for `CursorMut`).
205215

0 commit comments

Comments
 (0)