Skip to content

Commit 16cefab

Browse files
author
Ulrik Sverdrup
committed
linked_list: Add method Node::set_next
1 parent 201852e commit 16cefab

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

src/libcollections/linked_list.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,21 @@ impl<T> Node<T> {
140140
fn new(v: T) -> Node<T> {
141141
Node{value: v, next: None, prev: Rawlink::none()}
142142
}
143+
144+
/// Update the `prev` link on `next`, then set self's next pointer.
145+
///
146+
/// `self.next` should be `None` when you call this
147+
/// (otherwise a Node is probably being dropped by mistake).
148+
fn set_next(&mut self, mut next: Box<Node<T>>) {
149+
debug_assert!(self.next.is_none());
150+
next.prev = Rawlink::some(self);
151+
self.next = Some(next);
152+
}
143153
}
144154

145-
/// Set the .prev field on `next`, then return `Some(next)`
146-
fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
147-
-> Link<T> {
148-
next.prev = prev;
155+
/// Clear the .prev field on `next`, then return `Some(next)`
156+
fn link_no_prev<T>(mut next: Box<Node<T>>) -> Link<T> {
157+
next.prev = Rawlink::none();
149158
Some(next)
150159
}
151160

@@ -157,7 +166,7 @@ impl<T> LinkedList<T> {
157166
match self.list_head {
158167
None => {
159168
self.list_tail = Rawlink::some(&mut *new_head);
160-
self.list_head = link_with_prev(new_head, Rawlink::none());
169+
self.list_head = link_no_prev(new_head);
161170
}
162171
Some(ref mut head) => {
163172
new_head.prev = Rawlink::none();
@@ -175,7 +184,7 @@ impl<T> LinkedList<T> {
175184
self.list_head.take().map(|mut front_node| {
176185
self.length -= 1;
177186
match front_node.next.take() {
178-
Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
187+
Some(node) => self.list_head = link_no_prev(node),
179188
None => self.list_tail = Rawlink::none()
180189
}
181190
front_node
@@ -184,12 +193,12 @@ impl<T> LinkedList<T> {
184193

185194
/// Add a Node last in the list
186195
#[inline]
187-
fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
196+
fn push_back_node(&mut self, new_tail: Box<Node<T>>) {
188197
match unsafe { self.list_tail.resolve_mut() } {
189198
None => return self.push_front_node(new_tail),
190199
Some(tail) => {
191200
self.list_tail = Rawlink::some(&mut *new_tail);
192-
tail.next = link_with_prev(new_tail, Rawlink::some(tail));
201+
tail.set_next(new_tail);
193202
}
194203
}
195204
self.length += 1;
@@ -267,7 +276,7 @@ impl<T> LinkedList<T> {
267276
match other.list_head.take() {
268277
None => return,
269278
Some(node) => {
270-
tail.next = link_with_prev(node, self.list_tail);
279+
tail.set_next(node);
271280
self.list_tail = o_tail;
272281
self.length += o_length;
273282
}
@@ -758,8 +767,8 @@ impl<'a, A> IterMut<'a, A> {
758767
Some(prev) => prev,
759768
};
760769
let node_own = prev_node.next.take().unwrap();
761-
ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
762-
prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
770+
ins_node.set_next(node_own);
771+
prev_node.set_next(ins_node);
763772
self.list.length += 1;
764773
}
765774
}

0 commit comments

Comments
 (0)