-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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::extend_from_within
method under vec_extend_from_within
feature gate
#79015
Conversation
r? @KodrAus (rust_highfive has picked a reviewer for you, use r? to override) |
I agree with Simon that this function feels a bit ad-hoc. But if you want to add this functionality then I think append_copy_from_within is a better name. For such uncommonly used function a long but clear name is OK (by Zipf's law). |
To be clear, the implementation is written by @WanzenBug, not myself. I have authored the RFC in question, but not the implementation. The fact that the function feels niche is addressed in the RFC. TL;DR: this is a basic building block for many multimedia encoders/decoders, which they presently hand-roll using unsafe code and often get it wrong, leading to exploitable memory safety bugs. |
It should already be possible to specialize this using our If we don't want to try commit to specializing upfront then I'm ok with |
If this can be reliably specialized for The Performance is crucial for this function: if it leaves performance on the table people will go back to hand-rolling this using ad-hoc unsafe code and getting it wrong. |
Vec::append_from_within
method under vec_append_from_within
feature gateVec::extend_from_within
method under vec_extend_from_within
feature gate
@KodrAus I don't know the details of the I've also added a test which checks that the specialization was indeed used (so users may relay that it's cheap when |
@KodrAus waiting for your review on this |
☔ The latest upstream changes (presumably #80530) made this pull request unmergeable. Please resolve the merge conflicts. |
767a211
to
789dc50
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
@bors retry Looks spurious. cc @rust-lang/infra, I think I've seen this error a couple times now.
|
Oh wait, that was mingw-check that failed, sorry. Would still be great to find the cause though. @bors r- |
789dc50
to
3c5f78c
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
I've tried to force-push to rerun CI, hoping that it's a phantom error, but since the error didn't change, I guess the error is stable. |
☔ The latest upstream changes (presumably #80758) made this pull request unmergeable. Please resolve the merge conflicts. |
3c5f78c
to
3fcadb6
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
☔ The latest upstream changes (presumably #80746) made this pull request unmergeable. Please resolve the merge conflicts. |
d071e4d
to
d5c2211
Compare
Thanks @WaffleLapkin! This looks good to me. @bors r+ |
📌 Commit d5c2211 has been approved by |
@bors r- Just noticed we need a tracking issue! I'll sort that now. |
@bors r+ |
📌 Commit 125ec78 has been approved by |
Yay. Thanks, @KodrAus, for reviewing! |
☀️ Test successful - checks-actions |
Make Vec::split_at_spare_mut public This PR introduces a new method to the public API, under `vec_split_at_spare` feature gate: ```rust impl<T, A: Allocator> impl Vec<T, A> { pub fn split_at_spare_mut(&mut self) -> (&mut [T], &mut [MaybeUninit<T>]); } ``` The method returns 2 slices, one slice references the content of the vector, and the other references the remaining spare capacity. The method was previously implemented while adding `Vec::extend_from_within` in rust-lang#79015, and used to implement `Vec::spare_capacity_mut` (as the later is just a subset of former one). See also previous [discussion in `Vec::spare_capacity_mut` tracking issue](rust-lang#75017 (comment)). ## Unresolved questions - [ ] Should we consider changing the name? `split_at_spare_mut` doesn't seem like an intuitive name - [ ] Should we deprecate `Vec::spare_capacity_mut`? Any usecase of `Vec::spare_capacity_mut` can be replaced with `Vec::split_at_spare_mut` (but not vise-versa) r? `@KodrAus`
…lfJung Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidation The implementation was changed in rust-lang#79015. Later it was [pointed out](rust-lang#81944 (comment)) that the implementation invalidates pointers to the buffer (initialized elements) by creating a unique reference to the buffer. This PR reverts the implementation. r? `@RalfJung`
…lfJung Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidation The implementation was changed in rust-lang#79015. Later it was [pointed out](rust-lang#81944 (comment)) that the implementation invalidates pointers to the buffer (initialized elements) by creating a unique reference to the buffer. This PR reverts the implementation. r? ``@RalfJung``
…lfJung Revert `Vec::spare_capacity_mut` impl to prevent pointers invalidation The implementation was changed in rust-lang#79015. Later it was [pointed out](rust-lang#81944 (comment)) that the implementation invalidates pointers to the buffer (initialized elements) by creating a unique reference to the buffer. This PR reverts the implementation. r? ```@RalfJung```
Implement rust-lang/rfcs#2714
tl;dr
This PR adds a
extend_from_within
method toVec
which allows copying elements from a range to the end:Implementation notes
Originally I've copied @Shnatsel's implementation with some minor changes to support other ranges:
But then I've realized that this duplicates most of the code from (private)
Vec::append_elements
, so I've used it instead.Then I've applied @KodrAus suggestions from #79015 (comment).