-
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
Explicitly mention that Vec::reserve
is based on len not capacity
#39701
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -437,7 +437,9 @@ impl<T> Vec<T> { | |||||||||||||
|
||||||||||||||
/// Reserves capacity for at least `additional` more elements to be inserted | ||||||||||||||
/// in the given `Vec<T>`. The collection may reserve more space to avoid | ||||||||||||||
/// frequent reallocations. | ||||||||||||||
/// frequent reallocations. After calling `reserve`, capacity will be | ||||||||||||||
/// greater than or equal to `self.len() + additional`. Does nothing if | ||||||||||||||
/// capacity is already sufficient. | ||||||||||||||
/// | ||||||||||||||
/// # Panics | ||||||||||||||
/// | ||||||||||||||
|
@@ -456,8 +458,9 @@ impl<T> Vec<T> { | |||||||||||||
} | ||||||||||||||
|
||||||||||||||
/// Reserves the minimum capacity for exactly `additional` more elements to | ||||||||||||||
/// be inserted in the given `Vec<T>`. Does nothing if the capacity is already | ||||||||||||||
/// sufficient. | ||||||||||||||
/// be inserted in the given `Vec<T>`. After calling `reserve_exact`, | ||||||||||||||
/// capacity will be greater than or equal to `self.len() + additional`. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be "equal to" rather than "greater than or equal to" given that this "reserves the minimum capacity for [an exact number of additional elements]"? Or is there a reason to leave the bound open? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For what it's worth, here's a relevant comment in the underlying implementation: Lines 275 to 280 in b3937ea
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, there may be more backing memory. But the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're right. Don't want to mislead people with "greater than or equal to" since it doesn't seem accurate in this case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertion in example below has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sgrif I had presumed that @alexcrichton I had taken the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to change if y'all think it's necessary. I went with the most conservative statement (and the one that matches the code example). I think if this statement changes, the assertion in the example should change to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sgrif perhaps:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure we don't guarantee that at the moment. Do we want to start guaranteeing that now? Other collections have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, in that case let's just merge as-is. |
||||||||||||||
/// Does nothing if the capacity is already sufficient. | ||||||||||||||
/// | ||||||||||||||
/// Note that the allocator may give the collection more space than it | ||||||||||||||
/// requests. Therefore capacity can not be relied upon to be precisely | ||||||||||||||
|
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.
“not less than” is a better way to phrase “greater than or equal to”
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 personally prefer "greater than or equal to".