优化链表drop方法

pull/1049/head
ares 3 years ago
parent 91c3117c2b
commit 841aed83e2

@ -71,9 +71,8 @@ impl<T> Stack<T> {
impl<T> Drop for Stack<T> {
fn drop(&mut self) {
let mut cur_link = self.head.take();
while let Some(mut boxed_node) = cur_link {
cur_link = boxed_node.next.take();
while let Some(box_node) = self.head.take() {
self.head = box_node.next;
}
}
}

@ -137,9 +137,8 @@ impl Drop for Node {
```rust
impl Drop for List {
fn drop(&mut self) {
let mut cur_link = mem::replace(&mut self.head, Link::Empty);
while let Link::More(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.next, Link::Empty);
while let Link::More(box_node) = mem::replace(&mut self.head, Link::Empty) {
self.head = box_node.next;
// boxed_node 在这里超出作用域并被 drop,
// 由于它的 `next` 字段拥有的 `Node` 被设置为 Link::Empty,
// 因此这里并不会有无边界的递归发生
@ -222,10 +221,8 @@ impl List {
impl Drop for List {
fn drop(&mut self) {
let mut cur_link = mem::replace(&mut self.head, Link::Empty);
while let Link::More(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.next, Link::Empty);
while let Link::More(box_node) = mem::replace(&mut self.head, Link::Empty) {
self.head = box_node.next;
}
}
}

@ -193,9 +193,8 @@ impl<T> List<T> {
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut cur_link = self.head.take();
while let Some(mut boxed_node) = cur_link {
cur_link = boxed_node.next.take();
while let Some(box_node) = self.head.take() {
self.head = box_node.next;
}
}
}

@ -53,9 +53,8 @@ impl List {
impl Drop for List {
fn drop(&mut self) {
let mut cur_link = mem::replace(&mut self.head, None);
while let Some(mut boxed_node) = cur_link {
cur_link = mem::replace(&mut boxed_node.next, None);
while let Some(box_node) = mem::replace(&mut self.head, None) {
self.head = box_node.next;
}
}
}
@ -103,9 +102,8 @@ impl List {
impl Drop for List {
fn drop(&mut self) {
let mut cur_link = self.head.take();
while let Some(mut boxed_node) = cur_link {
cur_link = boxed_node.next.take();
while let Some(box_node) = self.head.take() {
self.head = box_node.next;
}
}
}
@ -177,9 +175,8 @@ impl<T> List<T> {
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut cur_link = self.head.take();
while let Some(mut boxed_node) = cur_link {
cur_link = boxed_node.next.take();
while let Some(box_node) = self.head.take() {
self.head = box_node.next;
}
}
}

@ -5,9 +5,8 @@
```rust
impl<T> Drop for List<T> {
fn drop(&mut self) {
let mut cur_link = self.head.take();
while let Some(mut boxed_node) = cur_link {
cur_link = boxed_node.next.take();
while let Some(box_node) = self.head.take() {
self.head = box_node.next;
}
}
}

Loading…
Cancel
Save