-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Explain how Vec::with_capacity is faithful #135933
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? @ibraheemdev rustbot has assigned @ibraheemdev. Use |
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.
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.
library/alloc/src/vec/mod.rs
Outdated
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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 |
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.
I incorporated this.
library/alloc/src/vec/mod.rs
Outdated
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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`. |
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.
Also incorporated this.
r? @workingjubilee because there were some open questions on the original PR. |
Could not assign reviewer from: |
Seems they are on vacation. r? libs-api because from what I understand this adds additional guarantees to the public API. |
@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. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry I meant to remove the one after Vec
, indeed. That's the one that is definitely wrong.
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.
Fixed.
Co-authored-by: Jubilee <[email protected]> and jmaargh
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. |
@bors r+ |
@bors rollup |
This is a revival of #99790 building on the prose of @workingjubilee and edits of @jmaargh. Closes #99385.