@@ -61,21 +61,21 @@ list many times. With the cursor interface, one can do the following:
61
61
62
62
``` rust
63
63
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
65
65
{
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
+ }
79
79
}
80
80
```
81
81
@@ -84,31 +84,31 @@ iterator, perform operations on it and collect. This is easier, however it still
84
84
requires much needless allocation.
85
85
86
86
For another example, consider code that was previously using ` IterMut `
87
- extensions.
87
+ extensions.
88
88
``` rust
89
89
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 );
98
98
}
99
99
```
100
100
This can be changed almost verbatim to ` CursorMut ` :
101
101
``` rust
102
102
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 );
112
112
}
113
113
```
114
114
In general, the cursor interface is not the easiest way to do something.
@@ -133,61 +133,61 @@ These would provide the following interface:
133
133
134
134
``` rust
135
135
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 >;
148
148
}
149
149
150
150
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 >;
191
191
}
192
192
```
193
193
One should closely consider the lifetimes in this interface. Both ` Cursor ` and
0 commit comments