]> git.lizzy.rs Git - rust.git/commitdiff
linked_list: Use a safe loop in Drop
authorUlrik Sverdrup <root@localhost>
Sat, 6 Jun 2015 12:26:39 +0000 (14:26 +0200)
committerUlrik Sverdrup <root@localhost>
Sat, 6 Jun 2015 12:26:39 +0000 (14:26 +0200)
src/libcollections/linked_list.rs

index 0bfbfd7337713f2c908a9a70a1f8590fb0e47583..b82a25544c930b09367426be0ff9abc60f13033d 100644 (file)
@@ -626,21 +626,13 @@ pub fn split_off(&mut self, at: usize) -> LinkedList<T> {
 #[stable(feature = "rust1", since = "1.0.0")]
 impl<T> Drop for LinkedList<T> {
     fn drop(&mut self) {
-        // Dissolve the linked_list in backwards direction
+        // Dissolve the linked_list in a loop.
         // Just dropping the list_head can lead to stack exhaustion
         // when length is >> 1_000_000
-        let mut tail = self.list_tail;
-        loop {
-            match tail.resolve() {
-                None => break,
-                Some(prev) => {
-                    prev.next.take(); // release Box<Node<T>>
-                    tail = prev.prev;
-                }
-            }
+        while let Some(mut head_) = self.list_head.take() {
+            self.list_head = head_.next.take();
         }
         self.length = 0;
-        self.list_head = None;
         self.list_tail = Rawlink::none();
     }
 }