Skip to content

Commit

Permalink
Rollup merge of rust-lang#96173 - jmaargh:jmaargh/with-capacity-doc-f…
Browse files Browse the repository at this point in the history
…ix, r=Dylan-DPC

Fix documentation for  `with_capacity` and `reserve` families of methods

Fixes rust-lang#95614

Documentation for the following methods
 - `with_capacity`
 - `with_capacity_in`
 - `with_capacity_and_hasher`
 - `reserve`
 - `reserve_exact`
 - `try_reserve`
 - `try_reserve_exact`

was inconsistent and often not entirely correct where they existed on the following types
- `Vec`
- `VecDeque`
- `String`
- `OsString`
- `PathBuf`
- `BinaryHeap`
- `HashSet`
- `HashMap`
- `BufWriter`
- `LineWriter`

since the allocator is allowed to allocate more than the requested capacity in all such cases, and will frequently "allocate" much more in the case of zero-sized types (I also checked `BufReader`, but there the docs appear to be accurate as it appears to actually allocate the exact capacity).

Some effort was made to make the documentation more consistent between types as well.
  • Loading branch information
compiler-errors authored Jun 23, 2022
2 parents 4334739 + 95dc353 commit fbdc798
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 130 deletions.
51 changes: 29 additions & 22 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,11 @@ impl<T: Ord> BinaryHeap<T> {
BinaryHeap { data: vec![] }
}

/// Creates an empty `BinaryHeap` with a specific capacity.
/// This preallocates enough memory for `capacity` elements,
/// so that the `BinaryHeap` does not have to be reallocated
/// until it contains at least that many values.
/// Creates an empty `BinaryHeap` with at least the specified capacity.
///
/// The binary heap will be able to hold at least `capacity` elements without
/// reallocating. This method is allowed to allocate for more elements than
/// `capacity`. If `capacity` is 0, the binary heap will not allocate.
///
/// # Examples
///
Expand Down Expand Up @@ -906,16 +907,18 @@ impl<T> BinaryHeap<T> {
self.data.capacity()
}

/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
/// given `BinaryHeap`. Does nothing if the capacity is already sufficient.
/// Reserves the minimum capacity for at least `additional` elements more than
/// the current length. Unlike [`reserve`], this will not
/// deliberately over-allocate to speculatively avoid frequent allocations.
/// After calling `reserve_exact`, capacity will be greater than or equal to
/// `self.len() + additional`. 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 minimal. Prefer [`reserve`] if future
/// insertions are expected.
/// [`reserve`]: BinaryHeap::reserve
///
/// # Panics
///
/// Panics if the new capacity overflows `usize`.
/// Panics if the new capacity overflows [`usize`].
///
/// # Examples
///
Expand All @@ -935,12 +938,15 @@ impl<T> BinaryHeap<T> {
self.data.reserve_exact(additional);
}

/// Reserves capacity for at least `additional` more elements to be inserted in the
/// `BinaryHeap`. The collection may reserve more space to avoid frequent reallocations.
/// Reserves capacity for at least `additional` elements more than the
/// current length. The allocator may reserve more space to speculatively
/// avoid frequent allocations. After calling `reserve`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Does nothing if capacity is already sufficient.
///
/// # Panics
///
/// Panics if the new capacity overflows `usize`.
/// Panics if the new capacity overflows [`usize`].
///
/// # Examples
///
Expand All @@ -958,10 +964,11 @@ impl<T> BinaryHeap<T> {
self.data.reserve(additional);
}

/// Tries to reserve the minimum capacity for exactly `additional`
/// elements to be inserted in the given `BinaryHeap<T>`. After calling
/// `try_reserve_exact`, capacity will be greater than or equal to
/// `self.len() + additional` if it returns `Ok(())`.
/// Tries to reserve the minimum capacity for at least `additional` elements
/// more than the current length. Unlike [`try_reserve`], this will not
/// deliberately over-allocate to speculatively avoid frequent allocations.
/// After calling `try_reserve_exact`, capacity will be greater than or
/// equal to `self.len() + additional` if it returns `Ok(())`.
/// Does nothing if the capacity is already sufficient.
///
/// Note that the allocator may give the collection more space than it
Expand Down Expand Up @@ -999,11 +1006,11 @@ impl<T> BinaryHeap<T> {
self.data.try_reserve_exact(additional)
}

/// Tries to reserve capacity for at least `additional` more elements to be inserted
/// in the given `BinaryHeap<T>`. The collection may reserve more space to avoid
/// frequent reallocations. After calling `try_reserve`, capacity will be
/// greater than or equal to `self.len() + additional`. Does nothing if
/// capacity is already sufficient.
/// Tries to reserve capacity for at least `additional` elements more than the
/// current length. The allocator may reserve more space to speculatively
/// avoid frequent allocations. After calling `try_reserve`, capacity will be
/// greater than or equal to `self.len() + additional` if it returns
/// `Ok(())`. Does nothing if capacity is already sufficient.
///
/// # Errors
///
Expand Down
16 changes: 8 additions & 8 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
self.cap() - 1
}

/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
/// Reserves the minimum capacity for at least `additional` more elements to be inserted in the
/// given deque. Does nothing if the capacity is already sufficient.
///
/// Note that the allocator may give the collection more space than it requests. Therefore
Expand Down Expand Up @@ -716,7 +716,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
}

/// Reserves capacity for at least `additional` more elements to be inserted in the given
/// deque. The collection may reserve more space to avoid frequent reallocations.
/// deque. The collection may reserve more space to speculatively avoid frequent reallocations.
///
/// # Panics
///
Expand Down Expand Up @@ -748,10 +748,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
}
}

/// Tries to reserve the minimum capacity for exactly `additional` more elements to
/// Tries to reserve the minimum capacity for at least `additional` more elements to
/// be inserted in the given deque. After calling `try_reserve_exact`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Does nothing if the capacity is already sufficient.
/// capacity will be greater than or equal to `self.len() + additional` if
/// it returns `Ok(())`. 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
Expand Down Expand Up @@ -791,10 +791,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
}

/// Tries to reserve capacity for at least `additional` more elements to be inserted
/// in the given deque. The collection may reserve more space to avoid
/// in the given deque. The collection may reserve more space to speculatively avoid
/// frequent reallocations. After calling `try_reserve`, capacity will be
/// greater than or equal to `self.len() + additional`. Does nothing if
/// capacity is already sufficient.
/// greater than or equal to `self.len() + additional` if it returns
/// `Ok(())`. Does nothing if capacity is already sufficient.
///
/// # Errors
///
Expand Down
70 changes: 35 additions & 35 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,13 @@ impl String {
String { vec: Vec::new() }
}

/// Creates a new empty `String` with a particular capacity.
/// Creates a new empty `String` with at least the specified capacity.
///
/// `String`s have an internal buffer to hold their data. The capacity is
/// the length of that buffer, and can be queried with the [`capacity`]
/// method. This method creates an empty `String`, but one with an initial
/// buffer that can hold `capacity` bytes. This is useful when you may be
/// appending a bunch of data to the `String`, reducing the number of
/// buffer that can hold at least `capacity` bytes. This is useful when you
/// may be appending a bunch of data to the `String`, reducing the number of
/// reallocations it needs to do.
///
/// [`capacity`]: String::capacity
Expand Down Expand Up @@ -979,21 +979,16 @@ impl String {
self.vec.capacity()
}

/// Ensures that this `String`'s capacity is at least `additional` bytes
/// larger than its length.
///
/// The capacity may be increased by more than `additional` bytes if it
/// chooses, to prevent frequent reallocations.
///
/// If you do not want this "at least" behavior, see the [`reserve_exact`]
/// method.
/// Reserves capacity for at least `additional` bytes more than the
/// current length. The allocator may reserve more space to speculatively
/// avoid frequent allocations. After calling `reserve`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Does nothing if capacity is already sufficient.
///
/// # Panics
///
/// Panics if the new capacity overflows [`usize`].
///
/// [`reserve_exact`]: String::reserve_exact
///
/// # Examples
///
/// Basic usage:
Expand All @@ -1013,15 +1008,16 @@ impl String {
/// s.push('a');
/// s.push('b');
///
/// // s now has a length of 2 and a capacity of 10
/// // s now has a length of 2 and a capacity of at least 10
/// let capacity = s.capacity();
/// assert_eq!(2, s.len());
/// assert_eq!(10, s.capacity());
/// assert!(capacity >= 10);
///
/// // Since we already have an extra 8 capacity, calling this...
/// // Since we already have at least an extra 8 capacity, calling this...
/// s.reserve(8);
///
/// // ... doesn't actually increase.
/// assert_eq!(10, s.capacity());
/// assert_eq!(capacity, s.capacity());
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
Expand All @@ -1030,17 +1026,18 @@ impl String {
self.vec.reserve(additional)
}

/// Ensures that this `String`'s capacity is `additional` bytes
/// larger than its length.
///
/// Consider using the [`reserve`] method unless you absolutely know
/// better than the allocator.
/// Reserves the minimum capacity for at least `additional` bytes more than
/// the current length. Unlike [`reserve`], this will not
/// deliberately over-allocate to speculatively avoid frequent allocations.
/// After calling `reserve_exact`, capacity will be greater than or equal to
/// `self.len() + additional`. Does nothing if the capacity is already
/// sufficient.
///
/// [`reserve`]: String::reserve
///
/// # Panics
///
/// Panics if the new capacity overflows `usize`.
/// Panics if the new capacity overflows [`usize`].
///
/// # Examples
///
Expand All @@ -1061,15 +1058,16 @@ impl String {
/// s.push('a');
/// s.push('b');
///
/// // s now has a length of 2 and a capacity of 10
/// // s now has a length of 2 and a capacity of at least 10
/// let capacity = s.capacity();
/// assert_eq!(2, s.len());
/// assert_eq!(10, s.capacity());
/// assert!(capacity >= 10);
///
/// // Since we already have an extra 8 capacity, calling this...
/// // Since we already have at least an extra 8 capacity, calling this...
/// s.reserve_exact(8);
///
/// // ... doesn't actually increase.
/// assert_eq!(10, s.capacity());
/// assert_eq!(capacity, s.capacity());
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
Expand All @@ -1078,11 +1076,11 @@ impl String {
self.vec.reserve_exact(additional)
}

/// Tries to reserve capacity for at least `additional` more elements to be inserted
/// in the given `String`. The collection may reserve more space to avoid
/// frequent reallocations. After calling `reserve`, capacity will be
/// greater than or equal to `self.len() + additional`. Does nothing if
/// capacity is already sufficient.
/// Tries to reserve capacity for at least `additional` bytes more than the
/// current length. The allocator may reserve more space to speculatively
/// avoid frequent allocations. After calling `try_reserve`, capacity will be
/// greater than or equal to `self.len() + additional` if it returns
/// `Ok(())`. Does nothing if capacity is already sufficient.
///
/// # Errors
///
Expand Down Expand Up @@ -1112,9 +1110,11 @@ impl String {
self.vec.try_reserve(additional)
}

/// Tries to reserve the minimum capacity for exactly `additional` more elements to
/// be inserted in the given `String`. After calling `try_reserve_exact`,
/// capacity will be greater than or equal to `self.len() + additional`.
/// Tries to reserve the minimum capacity for at least `additional` bytes
/// more than the current length. Unlike [`try_reserve`], this will not
/// deliberately over-allocate to speculatively avoid frequent allocations.
/// After calling `try_reserve_exact`, capacity will be greater than or
/// equal to `self.len() + additional` if it returns `Ok(())`.
/// Does nothing if the capacity is already sufficient.
///
/// Note that the allocator may give the collection more space than it
Expand Down
Loading

0 comments on commit fbdc798

Please sign in to comment.