-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Vec: avoid creating slices to the elements #61114
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
|
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
There's just too many things using |
684f4a5 to
487a108
Compare
src/liballoc/raw_vec.rs
Outdated
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.
Should this just return a NonNull<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.
Maybe? There might be a reason for why this looks the way it does. Possibly because NonNull is not very ergonomic.
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.
yeah, NonNull is only useful as a storage type, it's useless for doing actual work.
This seems like it would also be a problem for anyone outside liballoc who uses
Is that right? And the workaround used here (accessing the internal RawVec) isn't available outside liballoc, right? So perhaps we should add |
|
Yes that is correct. That's what Stacked Borrows says, anyway. ;) This might also be an indication that the model is too strict? The desired interaction with unsized types is not really clear. One custom DST are a thing, I doubt we want Stacked Borrows to run the arbitrary user code to compute the size... |
|
Assuming that this restriction on the use of |
|
That sounds very reasonable! I didn't do that here because I was not sure how that shadowing would work out. |
|
re: |
|
Also I agree with @rkruppe. If as_ptr is a semantic footgun for the implementation, it's a footgun for our users too. We should add the shadowing implementation (which works fine, Vec shadows slice methods in a few places already). |
|
I will have to make them insta-stable though to not regress people doing |
|
All right, I rewrote this PR to add shadowing methods instead. I also moved the |
| fn into_iter(mut self) -> IntoIter<T> { | ||
| unsafe { | ||
| let begin = self.as_mut_ptr(); | ||
| assume(!begin.is_null()); |
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.
as_mut_ptr now contains an assume so I think I can remove this? Unfortunately whoever added this did not leave a comment, because it seemed redundant before already.
|
r? @gankro |
|
@bors r+ |
|
@gankro: 🔑 Insufficient privileges: Not in reviewers |
|
@bors r=Gankro |
|
📌 Commit 428ab7e has been approved by |
Instead of
self.deref_mut().as_mut_ptr()to get a raw pointer to the buffer, useself.buf.ptr_mut(). This (a) avoids creating a unique reference to all existing elements without any need, and (b) creates a pointer that can actually be used for the entire buffer, and not just for the part of it covered byself.deref_mut().I also got worried about
RawVec::ptrreturning a*mut Tfrom an&self, so I added both a mutable and an immutable version.Cc @gankro in particular for the
assumechanges -- I don't know why that is not inUnique, but I moved it up fromVec::dereftoRawVec::ptrto avoid having to repeat it everywhere.Fixes #60847