-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add Vec::get_uninit_unchecked #70967
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @joshtriplett (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. Due to 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. |
This comment has been minimized.
This comment has been minimized.
04c9896
to
0348abf
Compare
This comment has been minimized.
This comment has been minimized.
fe80faa
to
2225c47
Compare
src/liballoc/vec.rs
Outdated
/// assert_eq!(&*x, &[0, 1, 2, 3]); | ||
/// ``` | ||
#[unstable(feature = "vec_get_uninit_unchecked", issue = "none")] | ||
pub fn get_uninit_unchecked(&mut self, index: usize) -> &mut MaybeUninit<T> { |
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.
pub fn get_uninit_unchecked(&mut self, index: usize) -> &mut MaybeUninit<T> { | |
pub unsafe fn get_uninit_unchecked(&mut self, index: usize) -> &mut MaybeUninit<T> { |
There are two ways in which you can violate soundness with this method:
- You provide
index
whereindex >= self.capacity()
. - You do
let elem = vec.get_uninit_unchecked(index); *elem = MaybeUninit::uninit(); drop(vec[index]);
.
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.
Also, you can have a memory leak if you forget to use vec.set_len(...)
afterwards in some circumstances; also worth noting in docs.
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Tagging libs, and assigning to a random libs team member. |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
…g* Improve the explanation in the assertion* UB and memory leak notes
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@Centril Thank you for your review! I cannot find broken links. How can I find them? With |
Rather than conflating unchecked indexing and conversion to impl<T> Vec<T> {
/// Returns the vector's spare capacity as a mutable slice of maybe-uninitialized values.
///
/// The length of the slice will be `self.capacity() - self.len()`.
pub fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] { ... }
} |
Ping from Triage: Hi @moshg - any updates? |
Ping from Triage: Closing due to inactivity. @moshg please reopen with updates. Thanks for the PR. |
Add Vec::spare_capacity_mut Returns the remaining spare capacity of the vector as a slice of `MaybeUninit<T>`. As suggested by @sfackler in rust-lang#70967 (comment). r? @sfackler
This is intended to reduce misusing
get_unchecked_mut
to access out of bounds.In addition, we can detect out-of-allocation with this method unlike
x.as_ptr_mut().add(index)
.I hope this to help
get_unchecked
to do bound check in debug build in the future. #36976Though immutable
get_uninit_unchecked
is useless, should I nameget_uninit_unchecked_mut
?