Skip to content

Commit 498c10a

Browse files
Convert tabs to space in RFC 2570 Linked List Cursors
Tabs render as 8 spaces on GitHub which is different from the standard Rust style (4 spaces).
1 parent 81c6deb commit 498c10a

File tree

1 file changed

+84
-84
lines changed

1 file changed

+84
-84
lines changed

text/2570-linked-list-cursors.md

Lines changed: 84 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ list many times. With the cursor interface, one can do the following:
6161

6262
``` rust
6363
fn remove_replace<T, P, F>(list: &mut LinkedList<T>, p: P, f: F)
64-
where P: Fn(&T) -> bool, F: Fn(T) -> T
64+
where P: Fn(&T) -> bool, F: Fn(T) -> T
6565
{
66-
let mut cursor = list.cursor_mut();
67-
// move to the first element, if it exists
68-
loop {
69-
let should_replace = match cursor.peek() {
70-
Some(element) => p(element),
71-
None => break,
72-
};
73-
if should_replace {
74-
let old_element = cursor.pop().unwrap();
75-
cursor.insert(f(old_element));
76-
}
77-
cursor.move_next();
78-
}
66+
let mut cursor = list.cursor_mut();
67+
// move to the first element, if it exists
68+
loop {
69+
let should_replace = match cursor.peek() {
70+
Some(element) => p(element),
71+
None => break,
72+
};
73+
if should_replace {
74+
let old_element = cursor.pop().unwrap();
75+
cursor.insert(f(old_element));
76+
}
77+
cursor.move_next();
78+
}
7979
}
8080
```
8181

@@ -84,31 +84,31 @@ iterator, perform operations on it and collect. This is easier, however it still
8484
requires much needless allocation.
8585

8686
For another example, consider code that was previously using `IterMut`
87-
extensions.
87+
extensions.
8888
``` rust
8989
fn main() {
90-
let mut list: LinkedList<_> = (0..10).collect();
91-
let mut iter = list.iter_mut();
92-
while let Some(x) = iter.next() {
93-
if x >= 5 {
94-
break;
95-
}
96-
}
97-
iter.insert_next(12);
90+
let mut list: LinkedList<_> = (0..10).collect();
91+
let mut iter = list.iter_mut();
92+
while let Some(x) = iter.next() {
93+
if x >= 5 {
94+
break;
95+
}
96+
}
97+
iter.insert_next(12);
9898
}
9999
```
100100
This can be changed almost verbatim to `CursorMut`:
101101
``` rust
102102
fn main() {
103-
let mut list: LinkedList<_> = (0..10).collect();
104-
let mut cursor = list.cursor_mut() {
105-
while let Some(x) = cursor.peek_next() {
106-
if x >= 5 {
107-
break;
108-
}
109-
cursor.move_next();
110-
}
111-
cursor.insert(12);
103+
let mut list: LinkedList<_> = (0..10).collect();
104+
let mut cursor = list.cursor_mut() {
105+
while let Some(x) = cursor.peek_next() {
106+
if x >= 5 {
107+
break;
108+
}
109+
cursor.move_next();
110+
}
111+
cursor.insert(12);
112112
}
113113
```
114114
In general, the cursor interface is not the easiest way to do something.
@@ -133,61 +133,61 @@ These would provide the following interface:
133133

134134
``` rust
135135
impl<'list, T> Cursor<'list, T> {
136-
/// Move to the subsequent element of the list if it exists or the empty
137-
/// element
138-
pub fn move_next(&mut self);
139-
/// Move to the previous element of the list
140-
pub fn move_prev(&mut self);
141-
142-
/// Get the current element
143-
pub fn current(&self) -> Option<&'list T>;
144-
/// Get the following element
145-
pub fn peek(&self) -> Option<&'list T>;
146-
/// Get the previous element
147-
pub fn peek_before(&self) -> Option<&'list T>;
136+
/// Move to the subsequent element of the list if it exists or the empty
137+
/// element
138+
pub fn move_next(&mut self);
139+
/// Move to the previous element of the list
140+
pub fn move_prev(&mut self);
141+
142+
/// Get the current element
143+
pub fn current(&self) -> Option<&'list T>;
144+
/// Get the following element
145+
pub fn peek(&self) -> Option<&'list T>;
146+
/// Get the previous element
147+
pub fn peek_before(&self) -> Option<&'list T>;
148148
}
149149

150150
impl<'list T> CursorMut<'list, T> {
151-
/// Move to the subsequent element of the list if it exists or the empty
152-
/// element
153-
pub fn move_next(&mut self);
154-
/// Move to the previous element of the list
155-
pub fn move_prev(&mut self);
156-
157-
/// Get the current element
158-
pub fn current(&mut self) -> Option<&mut T>;
159-
/// Get the next element
160-
pub fn peek(&mut self) -> Option<&mut T>;
161-
/// Get the previous element
162-
pub fn peek_before(&mut self) -> Option<&mut T>;
163-
164-
/// Get an immutable cursor at the current element
165-
pub fn as_cursor<'cm>(&'cm self) -> Cursor<'cm, T>;
166-
167-
// Now the list editing operations
168-
169-
/// Insert `item` after the cursor
170-
pub fn insert(&mut self, item: T);
171-
/// Insert `item` before the cursor
172-
pub fn insert_before(&mut self, item: T);
173-
174-
/// Remove and return the item following the cursor
175-
pub fn pop(&mut self) -> Option<T>;
176-
/// Remove and return the item before the cursor
177-
pub fn pop_before(&mut self) -> Option<T>;
178-
179-
/// Insert `list` between the current element and the next
180-
pub fn insert_list(&mut self, list: LinkedList<T>);
181-
/// Insert `list` between the previous element and current
182-
pub fn insert_list_before(&mut self, list: LinkedList<T>);
183-
184-
/// Split the list in two after the current element
185-
/// The returned list consists of all elements following the current one.
186-
// note: consuming the cursor is not necessary here, but it makes sense
187-
// given the interface
188-
pub fn split(self) -> LinkedList<T>;
189-
/// Split the list in two before the current element
190-
pub fn split_before(self) -> LinkedList<T>;
151+
/// Move to the subsequent element of the list if it exists or the empty
152+
/// element
153+
pub fn move_next(&mut self);
154+
/// Move to the previous element of the list
155+
pub fn move_prev(&mut self);
156+
157+
/// Get the current element
158+
pub fn current(&mut self) -> Option<&mut T>;
159+
/// Get the next element
160+
pub fn peek(&mut self) -> Option<&mut T>;
161+
/// Get the previous element
162+
pub fn peek_before(&mut self) -> Option<&mut T>;
163+
164+
/// Get an immutable cursor at the current element
165+
pub fn as_cursor<'cm>(&'cm self) -> Cursor<'cm, T>;
166+
167+
// Now the list editing operations
168+
169+
/// Insert `item` after the cursor
170+
pub fn insert(&mut self, item: T);
171+
/// Insert `item` before the cursor
172+
pub fn insert_before(&mut self, item: T);
173+
174+
/// Remove and return the item following the cursor
175+
pub fn pop(&mut self) -> Option<T>;
176+
/// Remove and return the item before the cursor
177+
pub fn pop_before(&mut self) -> Option<T>;
178+
179+
/// Insert `list` between the current element and the next
180+
pub fn insert_list(&mut self, list: LinkedList<T>);
181+
/// Insert `list` between the previous element and current
182+
pub fn insert_list_before(&mut self, list: LinkedList<T>);
183+
184+
/// Split the list in two after the current element
185+
/// The returned list consists of all elements following the current one.
186+
// note: consuming the cursor is not necessary here, but it makes sense
187+
// given the interface
188+
pub fn split(self) -> LinkedList<T>;
189+
/// Split the list in two before the current element
190+
pub fn split_before(self) -> LinkedList<T>;
191191
}
192192
```
193193
One should closely consider the lifetimes in this interface. Both `Cursor` and

0 commit comments

Comments
 (0)