Skip to content

Commit 8d06efb

Browse files
author
blake2-ppc
committed
dlist: Collect a common pattern into link_with_prev()
1 parent 7b1c577 commit 8d06efb

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/libextra/dlist.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ impl<T> Rawlink<T> {
9898
}
9999
}
100100

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+
101107
impl<T> Container for List<T> {
102108
/// O(1)
103109
fn is_empty(&self) -> bool {
@@ -216,20 +222,17 @@ impl<T> List<T> {
216222
///
217223
/// O(1)
218224
pub fn pop_front(&mut self) -> Option<T> {
219-
match self.list_head {
225+
match util::replace(&mut self.list_head, None) {
220226
None => None,
221-
ref mut head @ Some(*) => {
227+
Some(old_head) => {
222228
self.length -= 1;
223-
match *head.swap_unwrap() {
229+
match *old_head {
224230
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());
228232
Some(value)
229233
}
230234
Node{value: value, next: None, prev: _} => {
231235
self.list_tail = Rawlink::none();
232-
*head = None;
233236
Some(value)
234237
}
235238
}
@@ -247,9 +250,7 @@ impl<T> List<T> {
247250
match other {
248251
List{list_head: None, list_tail: _, length: _} => return,
249252
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);
253254
self.list_tail = o_tail;
254255
self.length += o_length;
255256
}
@@ -447,13 +448,10 @@ impl<'self, A> ListInsertCursor<A> for MutForwardIterator<'self, A> {
447448
None => return self.list.push_front(elt), // at head
448449
Some(prev) => prev,
449450
};
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));
457455
self.list.length += 1;
458456
}
459457
}

0 commit comments

Comments
 (0)