Skip to content

Commit 32037a5

Browse files
author
Ulrik Sverdrup
committed
linked_list: Add Rawlink::from
1 parent 16cefab commit 32037a5

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/libcollections/linked_list.rs

+14-12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ impl<T> Rawlink<T> {
129129
}
130130
}
131131

132+
impl<'a, T> From<&'a mut Link<T>> for Rawlink<Node<T>> {
133+
fn from(node: &'a mut Link<T>) -> Self {
134+
match node.as_mut() {
135+
None => Rawlink::none(),
136+
Some(ptr) => Rawlink::some(ptr),
137+
}
138+
}
139+
}
140+
132141
impl<T> Clone for Rawlink<T> {
133142
#[inline]
134143
fn clone(&self) -> Rawlink<T> {
@@ -165,8 +174,8 @@ impl<T> LinkedList<T> {
165174
fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
166175
match self.list_head {
167176
None => {
168-
self.list_tail = Rawlink::some(&mut *new_head);
169177
self.list_head = link_no_prev(new_head);
178+
self.list_tail = Rawlink::from(&mut self.list_head);
170179
}
171180
Some(ref mut head) => {
172181
new_head.prev = Rawlink::none();
@@ -197,8 +206,8 @@ impl<T> LinkedList<T> {
197206
match unsafe { self.list_tail.resolve_mut() } {
198207
None => return self.push_front_node(new_tail),
199208
Some(tail) => {
200-
self.list_tail = Rawlink::some(&mut *new_tail);
201209
tail.set_next(new_tail);
210+
self.list_tail = Rawlink::from(&mut tail.next);
202211
}
203212
}
204213
self.length += 1;
@@ -297,13 +306,9 @@ impl<T> LinkedList<T> {
297306
#[inline]
298307
#[stable(feature = "rust1", since = "1.0.0")]
299308
pub fn iter_mut(&mut self) -> IterMut<T> {
300-
let head_raw = match self.list_head {
301-
Some(ref mut h) => Rawlink::some(&mut **h),
302-
None => Rawlink::none(),
303-
};
304-
IterMut{
309+
IterMut {
305310
nelem: self.len(),
306-
head: head_raw,
311+
head: Rawlink::from(&mut self.list_head),
307312
tail: self.list_tail,
308313
list: self
309314
}
@@ -717,10 +722,7 @@ impl<'a, A> Iterator for IterMut<'a, A> {
717722
unsafe {
718723
self.head.resolve_mut().map(|next| {
719724
self.nelem -= 1;
720-
self.head = match next.next {
721-
Some(ref mut node) => Rawlink::some(&mut **node),
722-
None => Rawlink::none(),
723-
};
725+
self.head = Rawlink::from(&mut next.next);
724726
&mut next.value
725727
})
726728
}

0 commit comments

Comments
 (0)