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

Fix documentation for with_capacity and reserve families of methods #96173

Merged
merged 1 commit into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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