Skip to content

Rustonomicon: Fix bug in implementation of Vec::drain() #28265

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

Merged
merged 2 commits into from
Sep 6, 2015
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions src/doc/nomicon/vec-drain.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ impl<'a, T> Drop for Drain<'a, T> {

impl<T> Vec<T> {
pub fn drain(&mut self) -> Drain<T> {
// this is a mem::forget safety thing. If Drain is forgotten, we just
// leak the whole Vec's contents. Also we need to do this eventually
// anyway, so why not do it now?
self.len = 0;

unsafe {
let iter = RawValIter::new(&self);

// this is a mem::forget safety thing. If Drain is forgotten, we just
// leak the whole Vec's contents. Also we need to do this *eventually*
// anyway, so why not do it now?
self.len = 0;

Drain {
iter: RawValIter::new(&self),
iter: iter,
vec: PhantomData,
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/doc/nomicon/vec-final.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ impl<T> Vec<T> {
}

pub fn drain(&mut self) -> Drain<T> {
// this is a mem::forget safety thing. If this is forgotten, we just
// leak the whole Vec's contents. Also we need to do this *eventually*
// anyway, so why not do it now?
self.len = 0;
unsafe {
let iter = RawValIter::new(&self);

// this is a mem::forget safety thing. If Drain is forgotten, we just
// leak the whole Vec's contents. Also we need to do this *eventually*
// anyway, so why not do it now?
self.len = 0;

Drain {
iter: RawValIter::new(&self),
iter: iter,
vec: PhantomData,
}
}
Expand Down