]> git.lizzy.rs Git - rust.git/commitdiff
linked_list: Add Rawlink::from
authorUlrik Sverdrup <root@localhost>
Sat, 6 Jun 2015 12:26:40 +0000 (14:26 +0200)
committerUlrik Sverdrup <root@localhost>
Sat, 6 Jun 2015 18:05:39 +0000 (20:05 +0200)
src/libcollections/linked_list.rs

index 43cefeecdd9dc48e2c3ac940f213abfd79f7a1c3..980fe00f1e5b4e1b4c65fc59b2ea2c76a2fbc39e 100644 (file)
@@ -129,6 +129,15 @@ fn take(&mut self) -> Rawlink<T> {
     }
 }
 
+impl<'a, T> From<&'a mut Link<T>> for Rawlink<Node<T>> {
+    fn from(node: &'a mut Link<T>) -> Self {
+        match node.as_mut() {
+            None => Rawlink::none(),
+            Some(ptr) => Rawlink::some(ptr),
+        }
+    }
+}
+
 impl<T> Clone for Rawlink<T> {
     #[inline]
     fn clone(&self) -> Rawlink<T> {
@@ -165,8 +174,8 @@ impl<T> LinkedList<T> {
     fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
         match self.list_head {
             None => {
-                self.list_tail = Rawlink::some(&mut *new_head);
                 self.list_head = link_no_prev(new_head);
+                self.list_tail = Rawlink::from(&mut self.list_head);
             }
             Some(ref mut head) => {
                 new_head.prev = Rawlink::none();
@@ -197,8 +206,8 @@ fn push_back_node(&mut self, new_tail: Box<Node<T>>) {
         match unsafe { self.list_tail.resolve_mut() } {
             None => return self.push_front_node(new_tail),
             Some(tail) => {
-                self.list_tail = Rawlink::some(&mut *new_tail);
                 tail.set_next(new_tail);
+                self.list_tail = Rawlink::from(&mut tail.next);
             }
         }
         self.length += 1;
@@ -297,13 +306,9 @@ pub fn iter(&self) -> Iter<T> {
     #[inline]
     #[stable(feature = "rust1", since = "1.0.0")]
     pub fn iter_mut(&mut self) -> IterMut<T> {
-        let head_raw = match self.list_head {
-            Some(ref mut h) => Rawlink::some(&mut **h),
-            None => Rawlink::none(),
-        };
-        IterMut{
+        IterMut {
             nelem: self.len(),
-            head: head_raw,
+            head: Rawlink::from(&mut self.list_head),
             tail: self.list_tail,
             list: self
         }
@@ -717,10 +722,7 @@ fn next(&mut self) -> Option<&'a mut A> {
         unsafe {
             self.head.resolve_mut().map(|next| {
                 self.nelem -= 1;
-                self.head = match next.next {
-                    Some(ref mut node) => Rawlink::some(&mut **node),
-                    None => Rawlink::none(),
-                };
+                self.head = Rawlink::from(&mut next.next);
                 &mut next.value
             })
         }