Explain how Vec::with_capacity is faithful - #135933
Conversation
commented
Jan 23, 2025
|
r? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
left a comment
There was a problem hiding this comment.
Awesome! Not only is this very clear and understandable, it's also a good design that can make use of advanced allocator capabilities.
Linking #101316 for convenience.
| /// and no other size (such as, for example: a size rounded up to the nearest power of 2). | ||
| /// The allocator will return an allocation that is at least as large as requested, but it may be larger. | ||
| /// | ||
| /// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity |
There was a problem hiding this comment.
| /// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity | |
| /// It is guaranteed that the [`Vec::capacity`] method returns a value that is at least the requested capacity |
| /// It is guaranteed that the [Vec::capacity] method returns a value that is at least the requested capacity | ||
| /// and not more than the allocated capacity. | ||
| /// | ||
| /// The method [Vec::shrink_to_fit] can discard excess capacity an allocator has given to a `Vec`. |
There was a problem hiding this comment.
| /// The method [Vec::shrink_to_fit] can discard excess capacity an allocator has given to a `Vec`. | |
| /// The method [`Vec::shrink_to_fit`] can discard excess capacity an allocator has given to a `Vec`. |
commented
Jan 31, 2025
|
r? @workingjubilee because there were some open questions on the original PR. |
commented
Jan 31, 2025
|
Could not assign reviewer from: |
commented
Jan 31, 2025
|
Seems they are on vacation. r? libs-api because from what I understand this adds additional guarantees to the public API. |
commented
Jan 31, 2025
|
@rust-lang/libs-api: This PR adds the guarantee that Whether the actual allocation can hold a larger number of elements is up to the allocator. So this still only guarantees that Before this PR, we guaranteed |
|
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
commented
Feb 11, 2025
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
left a comment
There was a problem hiding this comment.
This is what I was aiming at, yes. Thank you for incorporating this and @jmaargh's edits.
| /// and from a [`Box<[T]>`][owned slice] without reallocating or moving the elements. | ||
| /// It is guaranteed, in order to respect the intentions of the programmer, that | ||
| /// all of `vec![e_1, e_2, ..., e_n]`, `vec![x; n]` and [`Vec::with_capacity(n)`] produce a `Vec`, | ||
| /// that requests an allocation of the exact size needed for precisely `n` elements from the allocator, |
There was a problem hiding this comment.
| /// that requests an allocation of the exact size needed for precisely `n` elements from the allocator, | |
| /// that requests an allocation of the exact size needed for precisely `n` elements from the allocator |
AFAIK there shouldn't be a comma here?
There was a problem hiding this comment.
If we replace this comma and the rest of the sentence with just a dot, then the sentence already says everything it should. Then if we want to re-add this clarifying clause, it seems logical to replace the dot with a comma and then the clause. Thus I think the comma is correct here, but I could be wrong.
I'm not sure I can justify the comma that comes before the part you quoted though...
There was a problem hiding this comment.
Ah sorry I meant to remove the one after Vec, indeed. That's the one that is definitely wrong.
Co-authored-by: Jubilee <workingjubilee@gmail.com> and jmaargh
commented
Feb 21, 2025
|
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
commented
Feb 24, 2025
|
@bors r+ |
commented
Feb 24, 2025
commented
Feb 24, 2025
|
@bors rollup |
This is a revival of #99790 building on the prose of @workingjubilee and edits of @jmaargh. Closes #99385.