Skip to content

Commit fbdc798

Browse files
Rollup merge of rust-lang#96173 - jmaargh:jmaargh/with-capacity-doc-fix, 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.
2 parents 4334739 + 95dc353 commit fbdc798

File tree

10 files changed

+191
-130
lines changed

10 files changed

+191
-130
lines changed

library/alloc/src/collections/binary_heap.rs

+29-22
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,11 @@ impl<T: Ord> BinaryHeap<T> {
374374
BinaryHeap { data: vec![] }
375375
}
376376

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

909-
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
910-
/// given `BinaryHeap`. Does nothing if the capacity is already sufficient.
910+
/// Reserves the minimum capacity for at least `additional` elements more than
911+
/// the current length. Unlike [`reserve`], this will not
912+
/// deliberately over-allocate to speculatively avoid frequent allocations.
913+
/// After calling `reserve_exact`, capacity will be greater than or equal to
914+
/// `self.len() + additional`. Does nothing if the capacity is already
915+
/// sufficient.
911916
///
912-
/// Note that the allocator may give the collection more space than it requests. Therefore
913-
/// capacity can not be relied upon to be precisely minimal. Prefer [`reserve`] if future
914-
/// insertions are expected.
917+
/// [`reserve`]: BinaryHeap::reserve
915918
///
916919
/// # Panics
917920
///
918-
/// Panics if the new capacity overflows `usize`.
921+
/// Panics if the new capacity overflows [`usize`].
919922
///
920923
/// # Examples
921924
///
@@ -935,12 +938,15 @@ impl<T> BinaryHeap<T> {
935938
self.data.reserve_exact(additional);
936939
}
937940

938-
/// Reserves capacity for at least `additional` more elements to be inserted in the
939-
/// `BinaryHeap`. The collection may reserve more space to avoid frequent reallocations.
941+
/// Reserves capacity for at least `additional` elements more than the
942+
/// current length. The allocator may reserve more space to speculatively
943+
/// avoid frequent allocations. After calling `reserve`,
944+
/// capacity will be greater than or equal to `self.len() + additional`.
945+
/// Does nothing if capacity is already sufficient.
940946
///
941947
/// # Panics
942948
///
943-
/// Panics if the new capacity overflows `usize`.
949+
/// Panics if the new capacity overflows [`usize`].
944950
///
945951
/// # Examples
946952
///
@@ -958,10 +964,11 @@ impl<T> BinaryHeap<T> {
958964
self.data.reserve(additional);
959965
}
960966

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

1002-
/// Tries to reserve capacity for at least `additional` more elements to be inserted
1003-
/// in the given `BinaryHeap<T>`. The collection may reserve more space to avoid
1004-
/// frequent reallocations. After calling `try_reserve`, capacity will be
1005-
/// greater than or equal to `self.len() + additional`. Does nothing if
1006-
/// capacity is already sufficient.
1009+
/// Tries to reserve capacity for at least `additional` elements more than the
1010+
/// current length. The allocator may reserve more space to speculatively
1011+
/// avoid frequent allocations. After calling `try_reserve`, capacity will be
1012+
/// greater than or equal to `self.len() + additional` if it returns
1013+
/// `Ok(())`. Does nothing if capacity is already sufficient.
10071014
///
10081015
/// # Errors
10091016
///

library/alloc/src/collections/vec_deque/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
688688
self.cap() - 1
689689
}
690690

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

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

751-
/// Tries to reserve the minimum capacity for exactly `additional` more elements to
751+
/// Tries to reserve the minimum capacity for at least `additional` more elements to
752752
/// be inserted in the given deque. After calling `try_reserve_exact`,
753-
/// capacity will be greater than or equal to `self.len() + additional`.
754-
/// Does nothing if the capacity is already sufficient.
753+
/// capacity will be greater than or equal to `self.len() + additional` if
754+
/// it returns `Ok(())`. Does nothing if the capacity is already sufficient.
755755
///
756756
/// Note that the allocator may give the collection more space than it
757757
/// requests. Therefore, capacity can not be relied upon to be precisely
@@ -791,10 +791,10 @@ impl<T, A: Allocator> VecDeque<T, A> {
791791
}
792792

793793
/// Tries to reserve capacity for at least `additional` more elements to be inserted
794-
/// in the given deque. The collection may reserve more space to avoid
794+
/// in the given deque. The collection may reserve more space to speculatively avoid
795795
/// frequent reallocations. After calling `try_reserve`, capacity will be
796-
/// greater than or equal to `self.len() + additional`. Does nothing if
797-
/// capacity is already sufficient.
796+
/// greater than or equal to `self.len() + additional` if it returns
797+
/// `Ok(())`. Does nothing if capacity is already sufficient.
798798
///
799799
/// # Errors
800800
///

library/alloc/src/string.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,13 @@ impl String {
455455
String { vec: Vec::new() }
456456
}
457457

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

982-
/// Ensures that this `String`'s capacity is at least `additional` bytes
983-
/// larger than its length.
984-
///
985-
/// The capacity may be increased by more than `additional` bytes if it
986-
/// chooses, to prevent frequent reallocations.
987-
///
988-
/// If you do not want this "at least" behavior, see the [`reserve_exact`]
989-
/// method.
982+
/// Reserves capacity for at least `additional` bytes more than the
983+
/// current length. The allocator may reserve more space to speculatively
984+
/// avoid frequent allocations. After calling `reserve`,
985+
/// capacity will be greater than or equal to `self.len() + additional`.
986+
/// Does nothing if capacity is already sufficient.
990987
///
991988
/// # Panics
992989
///
993990
/// Panics if the new capacity overflows [`usize`].
994991
///
995-
/// [`reserve_exact`]: String::reserve_exact
996-
///
997992
/// # Examples
998993
///
999994
/// Basic usage:
@@ -1013,15 +1008,16 @@ impl String {
10131008
/// s.push('a');
10141009
/// s.push('b');
10151010
///
1016-
/// // s now has a length of 2 and a capacity of 10
1011+
/// // s now has a length of 2 and a capacity of at least 10
1012+
/// let capacity = s.capacity();
10171013
/// assert_eq!(2, s.len());
1018-
/// assert_eq!(10, s.capacity());
1014+
/// assert!(capacity >= 10);
10191015
///
1020-
/// // Since we already have an extra 8 capacity, calling this...
1016+
/// // Since we already have at least an extra 8 capacity, calling this...
10211017
/// s.reserve(8);
10221018
///
10231019
/// // ... doesn't actually increase.
1024-
/// assert_eq!(10, s.capacity());
1020+
/// assert_eq!(capacity, s.capacity());
10251021
/// ```
10261022
#[cfg(not(no_global_oom_handling))]
10271023
#[inline]
@@ -1030,17 +1026,18 @@ impl String {
10301026
self.vec.reserve(additional)
10311027
}
10321028

1033-
/// Ensures that this `String`'s capacity is `additional` bytes
1034-
/// larger than its length.
1035-
///
1036-
/// Consider using the [`reserve`] method unless you absolutely know
1037-
/// better than the allocator.
1029+
/// Reserves the minimum capacity for at least `additional` bytes more than
1030+
/// the current length. Unlike [`reserve`], this will not
1031+
/// deliberately over-allocate to speculatively avoid frequent allocations.
1032+
/// After calling `reserve_exact`, capacity will be greater than or equal to
1033+
/// `self.len() + additional`. Does nothing if the capacity is already
1034+
/// sufficient.
10381035
///
10391036
/// [`reserve`]: String::reserve
10401037
///
10411038
/// # Panics
10421039
///
1043-
/// Panics if the new capacity overflows `usize`.
1040+
/// Panics if the new capacity overflows [`usize`].
10441041
///
10451042
/// # Examples
10461043
///
@@ -1061,15 +1058,16 @@ impl String {
10611058
/// s.push('a');
10621059
/// s.push('b');
10631060
///
1064-
/// // s now has a length of 2 and a capacity of 10
1061+
/// // s now has a length of 2 and a capacity of at least 10
1062+
/// let capacity = s.capacity();
10651063
/// assert_eq!(2, s.len());
1066-
/// assert_eq!(10, s.capacity());
1064+
/// assert!(capacity >= 10);
10671065
///
1068-
/// // Since we already have an extra 8 capacity, calling this...
1066+
/// // Since we already have at least an extra 8 capacity, calling this...
10691067
/// s.reserve_exact(8);
10701068
///
10711069
/// // ... doesn't actually increase.
1072-
/// assert_eq!(10, s.capacity());
1070+
/// assert_eq!(capacity, s.capacity());
10731071
/// ```
10741072
#[cfg(not(no_global_oom_handling))]
10751073
#[inline]
@@ -1078,11 +1076,11 @@ impl String {
10781076
self.vec.reserve_exact(additional)
10791077
}
10801078

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

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

0 commit comments

Comments
 (0)