@@ -98,6 +98,12 @@ impl<T> Rawlink<T> {
98
98
}
99
99
}
100
100
101
+ /// Set the .prev field on `next`, then return `Some(next)`
102
+ fn link_with_prev < T > ( mut next : ~Node < T > , prev : Rawlink < Node < T > > ) -> Link < T > {
103
+ next. prev = prev;
104
+ Some ( next)
105
+ }
106
+
101
107
impl < T > Container for List < T > {
102
108
/// O(1)
103
109
fn is_empty ( & self ) -> bool {
@@ -216,20 +222,17 @@ impl<T> List<T> {
216
222
///
217
223
/// O(1)
218
224
pub fn pop_front ( & mut self ) -> Option < T > {
219
- match self . list_head {
225
+ match util :: replace ( & mut self . list_head , None ) {
220
226
None => None ,
221
- ref mut head @ Some ( * ) => {
227
+ Some ( old_head ) => {
222
228
self . length -= 1 ;
223
- match * head . swap_unwrap ( ) {
229
+ match * old_head {
224
230
Node { value : value, next : Some ( next) , prev : _} => {
225
- let mut mnext = next;
226
- mnext. prev = Rawlink :: none ( ) ;
227
- * head = Some ( mnext) ;
231
+ self . list_head = link_with_prev ( next, Rawlink :: none ( ) ) ;
228
232
Some ( value)
229
233
}
230
234
Node { value : value, next : None , prev : _} => {
231
235
self . list_tail = Rawlink :: none ( ) ;
232
- * head = None ;
233
236
Some ( value)
234
237
}
235
238
}
@@ -247,9 +250,7 @@ impl<T> List<T> {
247
250
match other {
248
251
List { list_head : None , list_tail : _, length : _} => return ,
249
252
List { list_head : Some ( node) , list_tail : o_tail, length : o_length} => {
250
- let mut lnk_node = node;
251
- lnk_node. prev = self . list_tail ;
252
- tail. next = Some ( lnk_node) ;
253
+ tail. next = link_with_prev ( node, self . list_tail ) ;
253
254
self . list_tail = o_tail;
254
255
self . length += o_length;
255
256
}
@@ -447,13 +448,10 @@ impl<'self, A> ListInsertCursor<A> for MutForwardIterator<'self, A> {
447
448
None => return self . list . push_front ( elt) , // at head
448
449
Some ( prev) => prev,
449
450
} ;
450
- let mut node_own = prev_node. next . swap_unwrap ( ) ;
451
- let mut ins_node = ~Node { value : elt,
452
- next : None ,
453
- prev : Rawlink :: some ( prev_node) } ;
454
- node_own. prev = Rawlink :: some ( ins_node) ;
455
- ins_node. next = Some ( node_own) ;
456
- prev_node. next = Some ( ins_node) ;
451
+ let mut ins_node = ~Node { value : elt, next : None , prev : Rawlink :: none ( ) } ;
452
+ let node_own = prev_node. next . swap_unwrap ( ) ;
453
+ ins_node. next = link_with_prev ( node_own, Rawlink :: some ( ins_node) ) ;
454
+ prev_node. next = link_with_prev ( ins_node, Rawlink :: some ( prev_node) ) ;
457
455
self . list . length += 1 ;
458
456
}
459
457
}
0 commit comments