Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle drop zst #425

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/vec/vec-zsts.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,20 @@ impl<T> DoubleEndedIterator for RawValIter<T> {
```

And that's it. Iteration works!

One last thing that we need to take into account is that now when our vec gets dropped it deallocates the memory that was allocated during the time our vec was alive. With ZSTs we did not allocate any memory, and infact we never do. So right now we have unsoundness in our code where we still try deallocate a `NonNull::dangling()` ptr that we use to simulate the ZST in our vec, This means that we would cause an undefined behaviour if we try to deallocate something that we never allocated (obviously and for the right reasons). Lets fix tha, in our raw_vec we are going to tweak our Drop trait and check that we deallocate only types that are sized.
pwbh marked this conversation as resolved.
Show resolved Hide resolved

```rust,ignore
impl<T> Drop for RawVec<T> {
fn drop(&mut self) {
println!("RawVec<T> Drop called, deallocating memory");
if self.cap != 0 && std::mem::size_of::<T>() > 0 {
let layout = std::alloc::Layout::array::<T>(self.cap).unwrap();
unsafe {
std::alloc::dealloc(self.ptr.as_ptr() as *mut _, layout);
}
}
}
}
```