Skip to content

Commit b486793

Browse files
Rename some methods of Linked List Cursor
These changes is where everyone seems to agree on. - peek -> peek_next - peek_before -> peek_prev - insert -> insert_after - insert_list -> splice_after - insert_list_before -> splice_before - split -> split_after
1 parent 498c10a commit b486793

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

text/2570-linked-list-cursors.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ fn remove_replace<T, P, F>(list: &mut LinkedList<T>, p: P, f: F)
6666
let mut cursor = list.cursor_mut();
6767
// move to the first element, if it exists
6868
loop {
69-
let should_replace = match cursor.peek() {
69+
let should_replace = match cursor.peek_next() {
7070
Some(element) => p(element),
7171
None => break,
7272
};
7373
if should_replace {
7474
let old_element = cursor.pop().unwrap();
75-
cursor.insert(f(old_element));
75+
cursor.insert_after(f(old_element));
7676
}
7777
cursor.move_next();
7878
}
@@ -108,7 +108,7 @@ fn main() {
108108
}
109109
cursor.move_next();
110110
}
111-
cursor.insert(12);
111+
cursor.insert_after(12);
112112
}
113113
```
114114
In general, the cursor interface is not the easiest way to do something.
@@ -142,9 +142,9 @@ impl<'list, T> Cursor<'list, T> {
142142
/// Get the current element
143143
pub fn current(&self) -> Option<&'list T>;
144144
/// Get the following element
145-
pub fn peek(&self) -> Option<&'list T>;
145+
pub fn peek_next(&self) -> Option<&'list T>;
146146
/// Get the previous element
147-
pub fn peek_before(&self) -> Option<&'list T>;
147+
pub fn peek_prev(&self) -> Option<&'list T>;
148148
}
149149

150150
impl<'list T> CursorMut<'list, T> {
@@ -157,17 +157,17 @@ impl<'list T> CursorMut<'list, T> {
157157
/// Get the current element
158158
pub fn current(&mut self) -> Option<&mut T>;
159159
/// Get the next element
160-
pub fn peek(&mut self) -> Option<&mut T>;
160+
pub fn peek_next(&mut self) -> Option<&mut T>;
161161
/// Get the previous element
162-
pub fn peek_before(&mut self) -> Option<&mut T>;
162+
pub fn peek_prev(&mut self) -> Option<&mut T>;
163163

164164
/// Get an immutable cursor at the current element
165165
pub fn as_cursor<'cm>(&'cm self) -> Cursor<'cm, T>;
166166

167167
// Now the list editing operations
168168

169169
/// Insert `item` after the cursor
170-
pub fn insert(&mut self, item: T);
170+
pub fn insert_after(&mut self, item: T);
171171
/// Insert `item` before the cursor
172172
pub fn insert_before(&mut self, item: T);
173173

@@ -177,15 +177,15 @@ impl<'list T> CursorMut<'list, T> {
177177
pub fn pop_before(&mut self) -> Option<T>;
178178

179179
/// Insert `list` between the current element and the next
180-
pub fn insert_list(&mut self, list: LinkedList<T>);
180+
pub fn splice_after(&mut self, list: LinkedList<T>);
181181
/// Insert `list` between the previous element and current
182-
pub fn insert_list_before(&mut self, list: LinkedList<T>);
182+
pub fn splice_before(&mut self, list: LinkedList<T>);
183183

184184
/// Split the list in two after the current element
185185
/// The returned list consists of all elements following the current one.
186186
// note: consuming the cursor is not necessary here, but it makes sense
187187
// given the interface
188-
pub fn split(self) -> LinkedList<T>;
188+
pub fn split_after(self) -> LinkedList<T>;
189189
/// Split the list in two before the current element
190190
pub fn split_before(self) -> LinkedList<T>;
191191
}
@@ -195,17 +195,17 @@ One should closely consider the lifetimes in this interface. Both `Cursor` and
195195
the annotation of `'list`.
196196

197197
The lifetime elision for their constructors is correct as
198-
```
198+
```rust
199199
pub fn cursor(&self) -> Cursor<T>
200200
```
201201
becomes
202-
```
202+
```rust
203203
pub fn cursor<'list>(&'list self) -> Cursor<'list, T>
204204
```
205205
which is what we would expect. (the same goes for `CursorMut`).
206206

207-
Since `Cursor` cannot mutate its list, `current`, `peek` and `peek_before` all
208-
live as long as `'list`. However, in `CursorMut` we must be careful to make
207+
Since `Cursor` cannot mutate its list, `current`, `peek_next` and `peek_prev`
208+
all live as long as `'list`. However, in `CursorMut` we must be careful to make
209209
these methods borrow. Otherwise, one could produce multiple mutable references
210210
to the same element.
211211

0 commit comments

Comments
 (0)