-
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
Make Vec::split_at_spare_mut public #81687
Conversation
This commit 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`, and used to implement `Vec::spare_capacity_mut` (as the later is just a subset of former one).
Thanks @WaffleLapkin! It might be worth noting that this is a low-level API, and if all you want to do is append data to a |
ping @KodrAus I've addressed your comment. |
@bors r+ rollup |
📌 Commit 8ff7b75 has been approved by |
Rollup of 11 pull requests Successful merges: - rust-lang#79849 (Clarify docs regarding sleep of zero duration) - rust-lang#80438 (Add `Box::into_inner`.) - rust-lang#81466 (Add suggest mut method for loop) - rust-lang#81687 (Make Vec::split_at_spare_mut public) - rust-lang#81904 (Bump stabilization version for const int methods) - rust-lang#81909 ([compiler/rustc_typeck/src/check/expr.rs] Remove unnecessary refs in pattern matching) - rust-lang#81910 (Use format string in bootstrap panic instead of a string directly) - rust-lang#81913 (Rename HIR UnOp variants) - rust-lang#81925 (Add long explanation for E0547) - rust-lang#81926 (add suggestion to use the `async_recursion` crate) - rust-lang#81951 (Update cargo) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
🚲 I think this can replace It would be good if the function had Vec has |
Yeah, I agree that it would be better to just have one method for getting spare capacity. Though I'm not sure how to discuss this replacement properly... |
This PR introduces a new method to the public API, under
vec_split_at_spare
feature gate: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 #79015,and used to implement
Vec::spare_capacity_mut
(as the later is just asubset of former one).
See also previous discussion in
Vec::spare_capacity_mut
tracking issue.Unresolved questions
split_at_spare_mut
doesn't seem like an intuitive nameVec::spare_capacity_mut
? Any usecase ofVec::spare_capacity_mut
can be replaced withVec::split_at_spare_mut
(but not vise-versa)r? @KodrAus