@@ -63,7 +63,7 @@ list many times. With the cursor interface, one can do the following:
63
63
fn remove_replace <T , P , F >(list : & mut LinkedList <T >, p : P , f : F )
64
64
where P : Fn (& T ) -> bool , F : Fn (T ) -> T
65
65
{
66
- let mut cursor = list . cursor_mut ();
66
+ let mut cursor = list . cursor_front_mut ();
67
67
// move to the first element, if it exists
68
68
loop {
69
69
let should_replace = match cursor . peek_next () {
@@ -101,7 +101,7 @@ This can be changed almost verbatim to `CursorMut`:
101
101
``` rust
102
102
fn main () {
103
103
let mut list : LinkedList <_ > = (0 .. 10 ). collect ();
104
- let mut cursor = list . cursor_mut () {
104
+ let mut cursor = list . cursor_front_mut () {
105
105
while let Some (x ) = cursor . peek_next () {
106
106
if x >= 5 {
107
107
break ;
@@ -122,17 +122,26 @@ One gets a cursor the exact same way as one would get an iterator. The
122
122
returned cursor would point to the "empty" element, i.e. if you got an element
123
123
and called ` current ` you would receive ` None ` .
124
124
``` 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 >;
127
127
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 >;
130
136
```
131
137
132
138
These would provide the following interface:
133
139
134
140
``` rust
135
141
impl <'list , T > Cursor <'list , T > {
142
+ /// Returns the cursor position index within the `LinkedList`.
143
+ pub fn index (& self ) -> Option <usize >;
144
+
136
145
/// Move to the subsequent element of the list if it exists or the empty
137
146
/// element
138
147
pub fn move_next (& mut self );
@@ -148,6 +157,9 @@ impl<'list, T> Cursor<'list, T> {
148
157
}
149
158
150
159
impl <'list T > CursorMut <'list , T > {
160
+ /// Returns the cursor position index within the `LinkedList`.
161
+ pub fn index (& self ) -> Option <usize >;
162
+
151
163
/// Move to the subsequent element of the list if it exists or the empty
152
164
/// element
153
165
pub fn move_next (& mut self );
@@ -182,11 +194,9 @@ impl<'list T> CursorMut<'list, T> {
182
194
183
195
/// Split the list in two after the current element
184
196
/// 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 >;
188
198
/// 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 >;
190
200
}
191
201
```
192
202
One should closely consider the lifetimes in this interface. Both ` Cursor ` and
@@ -195,11 +205,11 @@ the annotation of `'list`.
195
205
196
206
The lifetime elision for their constructors is correct as
197
207
``` rust
198
- pub fn cursor (& self ) -> Cursor <T >
208
+ pub fn cursor_front (& self ) -> Cursor <T >
199
209
```
200
210
becomes
201
211
```rust
202
- pub fn cursor <'list >(& 'list self ) -> Cursor <'list , T >
212
+ pub fn cursor_front <'list >(& 'list self ) -> Cursor <'list , T >
203
213
```
204
214
which is what we would expect . (the same goes for `CursorMut `).
205
215
0 commit comments