Skip to content
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

Merged
merged 4 commits into from
Feb 10, 2021
Merged

Conversation

WaffleLapkin
Copy link
Member

This PR introduces a new method to the public API, under
vec_split_at_spare feature gate:

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 #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.

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

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).
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 2, 2021
@KodrAus
Copy link
Contributor

KodrAus commented Feb 3, 2021

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 Vec you can use push, extend etc.

@WaffleLapkin
Copy link
Member Author

ping @KodrAus I've addressed your comment.

@KodrAus
Copy link
Contributor

KodrAus commented Feb 9, 2021

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 9, 2021

📌 Commit 8ff7b75 has been approved by KodrAus

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 9, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 10, 2021
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
@bors bors merged commit e2765f8 into rust-lang:master Feb 10, 2021
@rustbot rustbot added this to the 1.52.0 milestone Feb 10, 2021
@WaffleLapkin WaffleLapkin deleted the split_at_spare branch February 10, 2021 10:09
@kornelski
Copy link
Contributor

kornelski commented Feb 19, 2021

🚲

I think this can replace spare_capacity_mut entirely, even reuse the same name.

It would be good if the function had capacity in its name, because spare alone isn't used anywhere else in Vec's docs, so it isn't part of the vocabulary.

Vec has split_off, so another split_ method that doesn't modify the Vec is slightly confusing.

@WaffleLapkin
Copy link
Member Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants