Closed kobeHub closed 2 years ago
Assuming that
Node
:
Data
Next: Box<Node>
L: List
: A: Node -> B: Node -> ...
Then
Drop L
Drop A
Drop A.Data
Drop A.Next->B
Drop A.Next->B.Data
Drop A.Next->B.Next->...
...
Drop A.Next->B.Next
Drop A.Next
Not
Drop L
Drop A
Drop A.Data
Drop A.Next
Drop A.Next->B
Drop B.Data
Drop B.Next
Drop B.Next->...
...
In standard library, you will see
unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {}
Note the #[may_dangle]
.
The way I understand it: the list can clear the data properly using the default drop implementation if the list size is not large enough to blow the stack because it is recursive. But if we can be sure that the default drop implementation is tail-recursive then the compiler/runtime magic supposedly can avoid blowing the stack... but it isn't tail-recursive, as explained above by @makisevon , so a custom drop implementation is needed to avoid blowing the stack.
I got it, thanks for your explaination!
I'm confused about the 2.8 list drop manully implementation. The drop code for
Box<T>
runs, this first calls drop on theT
.Anyway
Box
should clear the data properly, is there something wrong or I misunderstand? Can someone explain that a little bit, thank you.