-
Notifications
You must be signed in to change notification settings - Fork 13.4k
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
Conversation
In the last code snippet on the following page there is a bug in the implementation of Vec::drain(). https://doc.rust-lang.org/nightly/nomicon/vec-drain.html ```rust pub fn drain(&mut self) -> Drain<T> { // Oops, setting it to 0 while we still need the old value! self.len = 0; unsafe { Drain { // len is used to create a &[T] from &self here, // so we end up always creating an empty slice. iter: RawValIter::new(&self), vec: PhantomData, } } } ``` A simple test to verify that Drain is broken can be found here: https://play.rust-lang.org/?gist=30f579565e4bbf4836ce&version=nightly And here's one with a fixed implementation: https://play.rust-lang.org/?gist=2ec0c1a6dcf5defd7a53&version=nightly
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
unsafe { | ||
let iter = RawValIter::new(&self); | ||
|
||
// this is a mem::forget safety thing. If this is forgotten, we just |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to keep saying Drain
here instead of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, guess I copied that from vec-final.
@bors r+ Thank you! |
📌 Commit 97dd250 has been approved by |
In the last code snippet on the following page there is a bug in the implementation of Vec::drain(). https://doc.rust-lang.org/nightly/nomicon/vec-drain.html ```rust pub fn drain(&mut self) -> Drain<T> { // Oops, setting it to 0 while we still need the old value! self.len = 0; unsafe { Drain { // len is used to create a &[T] from &self here, // so we end up always creating an empty slice. iter: RawValIter::new(&self), vec: PhantomData, } } } ``` A simple test to verify that Drain is broken can be found here: https://play.rust-lang.org/?gist=30f579565e4bbf4836ce&version=nightly And here's one with a fixed implementation: https://play.rust-lang.org/?gist=2ec0c1a6dcf5defd7a53&version=nightly
In the last code snippet on the following page there is a bug in the
implementation of Vec::drain().
https://doc.rust-lang.org/nightly/nomicon/vec-drain.html
A simple test to verify that Drain is broken can be found here:
https://play.rust-lang.org/?gist=30f579565e4bbf4836ce&version=nightly
And here's one with a fixed implementation:
https://play.rust-lang.org/?gist=2ec0c1a6dcf5defd7a53&version=nightly