mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 07:14:28 +00:00
Rollup merge of #96173 - jmaargh:jmaargh/with-capacity-doc-fix, r=Dylan-DPC
Fix documentation for `with_capacity` and `reserve` families of methods Fixes #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.
This commit is contained in:
commit
262382ff37
@ -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
|
||||
///
|
||||
@ -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
|
||||
///
|
||||
@ -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
|
||||
///
|
||||
@ -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
|
||||
@ -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
|
||||
///
|
||||
|
@ -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
|
||||
@ -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
|
||||
///
|
||||
@ -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
|
||||
@ -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
|
||||
///
|
||||
|
@ -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
|
||||
@ -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:
|
||||
@ -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]
|
||||
@ -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
|
||||
///
|
||||
@ -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]
|
||||
@ -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
|
||||
///
|
||||
@ -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
|
||||
|
@ -425,17 +425,25 @@ impl<T> Vec<T> {
|
||||
Vec { buf: RawVec::NEW, len: 0 }
|
||||
}
|
||||
|
||||
/// Constructs a new, empty `Vec<T>` with the specified capacity.
|
||||
/// Constructs a new, empty `Vec<T>` with at least the specified capacity.
|
||||
///
|
||||
/// The vector will be able to hold exactly `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the vector will not allocate.
|
||||
/// The vector 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 vector will not allocate.
|
||||
///
|
||||
/// It is important to note that although the returned vector has the
|
||||
/// *capacity* specified, the vector will have a zero *length*. For an
|
||||
/// explanation of the difference between length and capacity, see
|
||||
/// minimum *capacity* specified, the vector will have a zero *length*. For
|
||||
/// an explanation of the difference between length and capacity, see
|
||||
/// *[Capacity and reallocation]*.
|
||||
///
|
||||
/// If it is imporant to know the exact allocated capacity of a `Vec`,
|
||||
/// always use the [`capacity`] method after construction.
|
||||
///
|
||||
/// For `Vec<T>` where `T` is a zero-sized type, there will be no allocation
|
||||
/// and the capacity will always be `usize::MAX`.
|
||||
///
|
||||
/// [Capacity and reallocation]: #capacity-and-reallocation
|
||||
/// [`capacity`]: Vec::capacity
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
@ -448,19 +456,24 @@ impl<T> Vec<T> {
|
||||
///
|
||||
/// // The vector contains no items, even though it has capacity for more
|
||||
/// assert_eq!(vec.len(), 0);
|
||||
/// assert_eq!(vec.capacity(), 10);
|
||||
/// assert!(vec.capacity() >= 10);
|
||||
///
|
||||
/// // These are all done without reallocating...
|
||||
/// for i in 0..10 {
|
||||
/// vec.push(i);
|
||||
/// }
|
||||
/// assert_eq!(vec.len(), 10);
|
||||
/// assert_eq!(vec.capacity(), 10);
|
||||
/// assert!(vec.capacity() >= 10);
|
||||
///
|
||||
/// // ...but this may make the vector reallocate
|
||||
/// vec.push(11);
|
||||
/// assert_eq!(vec.len(), 11);
|
||||
/// assert!(vec.capacity() >= 11);
|
||||
///
|
||||
/// // A vector of a zero-sized type will always over-allocate, since no
|
||||
/// // allocation is necessary
|
||||
/// let vec_units = Vec::<()>::with_capacity(10);
|
||||
/// assert_eq!(vec_units.capacity(), usize::MAX);
|
||||
/// ```
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[inline]
|
||||
@ -566,18 +579,26 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
Vec { buf: RawVec::new_in(alloc), len: 0 }
|
||||
}
|
||||
|
||||
/// Constructs a new, empty `Vec<T, A>` with the specified capacity with the provided
|
||||
/// allocator.
|
||||
/// Constructs a new, empty `Vec<T, A>` with at least the specified capacity
|
||||
/// with the provided allocator.
|
||||
///
|
||||
/// The vector will be able to hold exactly `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the vector will not allocate.
|
||||
/// The vector 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 vector will not allocate.
|
||||
///
|
||||
/// It is important to note that although the returned vector has the
|
||||
/// *capacity* specified, the vector will have a zero *length*. For an
|
||||
/// explanation of the difference between length and capacity, see
|
||||
/// minimum *capacity* specified, the vector will have a zero *length*. For
|
||||
/// an explanation of the difference between length and capacity, see
|
||||
/// *[Capacity and reallocation]*.
|
||||
///
|
||||
/// If it is imporant to know the exact allocated capacity of a `Vec`,
|
||||
/// always use the [`capacity`] method after construction.
|
||||
///
|
||||
/// For `Vec<T, A>` where `T` is a zero-sized type, there will be no allocation
|
||||
/// and the capacity will always be `usize::MAX`.
|
||||
///
|
||||
/// [Capacity and reallocation]: #capacity-and-reallocation
|
||||
/// [`capacity`]: Vec::capacity
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
@ -607,6 +628,11 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
/// vec.push(11);
|
||||
/// assert_eq!(vec.len(), 11);
|
||||
/// assert!(vec.capacity() >= 11);
|
||||
///
|
||||
/// // A vector of a zero-sized type will always over-allocate, since no
|
||||
/// // allocation is necessary
|
||||
/// let vec_units = Vec::<(), System>::with_capacity_in(10, System);
|
||||
/// assert_eq!(vec_units.capacity(), usize::MAX);
|
||||
/// ```
|
||||
#[cfg(not(no_global_oom_handling))]
|
||||
#[inline]
|
||||
@ -793,10 +819,10 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
}
|
||||
|
||||
/// 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. After calling `reserve`, capacity will be
|
||||
/// greater than or equal to `self.len() + additional`. Does nothing if
|
||||
/// capacity is already sufficient.
|
||||
/// in the given `Vec<T>`. The collection may reserve more space to
|
||||
/// speculatively avoid frequent reallocations. After calling `reserve`,
|
||||
/// capacity will be greater than or equal to `self.len() + additional`.
|
||||
/// Does nothing if capacity is already sufficient.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
@ -815,10 +841,12 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
self.buf.reserve(self.len, additional);
|
||||
}
|
||||
|
||||
/// Reserves the minimum capacity for exactly `additional` more elements to
|
||||
/// be inserted in the given `Vec<T>`. After calling `reserve_exact`,
|
||||
/// capacity will be greater than or equal to `self.len() + additional`.
|
||||
/// Does nothing if the capacity is already sufficient.
|
||||
/// Reserves the minimum capacity for at least `additional` more elements to
|
||||
/// be inserted in the given `Vec<T>`. 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
|
||||
@ -844,10 +872,10 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
}
|
||||
|
||||
/// Tries to reserve capacity for at least `additional` more elements to be inserted
|
||||
/// in the given `Vec<T>`. The collection may reserve more space to avoid
|
||||
/// in the given `Vec<T>`. 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
|
||||
///
|
||||
@ -879,10 +907,11 @@ impl<T, A: Allocator> Vec<T, A> {
|
||||
self.buf.try_reserve(self.len, additional)
|
||||
}
|
||||
|
||||
/// Tries to reserve the minimum capacity for exactly `additional`
|
||||
/// elements to be inserted in the given `Vec<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 to be inserted in the given `Vec<T>`. 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
|
||||
|
@ -2128,6 +2128,15 @@ fn test_vec_cycle_wrapped() {
|
||||
c3.refs.v[1].set(Some(&c2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_sized_capacity() {
|
||||
for len in [0, 1, 2, 4, 8, 16, 32, 64, 128, 256] {
|
||||
let v = Vec::<()>::with_capacity(len);
|
||||
assert_eq!(v.len(), 0);
|
||||
assert_eq!(v.capacity(), usize::MAX);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_zero_sized_vec_push() {
|
||||
const N: usize = 8;
|
||||
|
@ -233,10 +233,11 @@ impl<K, V> HashMap<K, V, RandomState> {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Creates an empty `HashMap` with the specified capacity.
|
||||
/// Creates an empty `HashMap` with at least the specified capacity.
|
||||
///
|
||||
/// The hash map will be able to hold at least `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the hash map will not allocate.
|
||||
/// reallocating. This method is allowed to allocate for more elements than
|
||||
/// `capacity`. If `capacity` is 0, the hash set will not allocate.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@ -282,18 +283,19 @@ impl<K, V, S> HashMap<K, V, S> {
|
||||
HashMap { base: base::HashMap::with_hasher(hash_builder) }
|
||||
}
|
||||
|
||||
/// Creates an empty `HashMap` with the specified capacity, using `hash_builder`
|
||||
/// to hash the keys.
|
||||
/// Creates an empty `HashMap` with at least the specified capacity, using
|
||||
/// `hasher` to hash the keys.
|
||||
///
|
||||
/// The hash map will be able to hold at least `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the hash map will not allocate.
|
||||
/// reallocating. This method is allowed to allocate for more elements than
|
||||
/// `capacity`. If `capacity` is 0, the hash map will not allocate.
|
||||
///
|
||||
/// Warning: `hash_builder` is normally randomly generated, and
|
||||
/// Warning: `hasher` is normally randomly generated, and
|
||||
/// is designed to allow HashMaps to be resistant to attacks that
|
||||
/// cause many collisions and very poor performance. Setting it
|
||||
/// manually using this function can expose a DoS attack vector.
|
||||
///
|
||||
/// The `hash_builder` passed should implement the [`BuildHasher`] trait for
|
||||
/// The `hasher` passed should implement the [`BuildHasher`] trait for
|
||||
/// the HashMap to be useful, see its documentation for details.
|
||||
///
|
||||
/// # Examples
|
||||
@ -308,8 +310,8 @@ impl<K, V, S> HashMap<K, V, S> {
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
|
||||
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
|
||||
HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hash_builder) }
|
||||
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashMap<K, V, S> {
|
||||
HashMap { base: base::HashMap::with_capacity_and_hasher(capacity, hasher) }
|
||||
}
|
||||
|
||||
/// Returns the number of elements the map can hold without reallocating.
|
||||
@ -731,8 +733,10 @@ where
|
||||
S: BuildHasher,
|
||||
{
|
||||
/// Reserves capacity for at least `additional` more elements to be inserted
|
||||
/// in the `HashMap`. The collection may reserve more space to avoid
|
||||
/// frequent reallocations.
|
||||
/// in the `HashMap`. The collection may reserve more space to speculatively
|
||||
/// avoid frequent reallocations. After calling `reserve`,
|
||||
/// capacity will be greater than or equal to `self.len() + additional`.
|
||||
/// Does nothing if capacity is already sufficient.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
@ -752,8 +756,11 @@ where
|
||||
}
|
||||
|
||||
/// Tries to reserve capacity for at least `additional` more elements to be inserted
|
||||
/// in the given `HashMap<K, V>`. The collection may reserve more space to avoid
|
||||
/// frequent reallocations.
|
||||
/// in the `HashMap`. The collection may reserve more space to speculatively
|
||||
/// avoid frequent reallocations. After calling `reserve`,
|
||||
/// capacity will be greater than or equal to `self.len() + additional` if
|
||||
/// it returns `Ok(())`.
|
||||
/// Does nothing if capacity is already sufficient.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
@ -766,7 +773,7 @@ where
|
||||
/// use std::collections::HashMap;
|
||||
///
|
||||
/// let mut map: HashMap<&str, isize> = HashMap::new();
|
||||
/// map.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
|
||||
/// map.try_reserve(10).expect("why is the test harness OOMing on a handful of bytes?");
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "try_reserve", since = "1.57.0")]
|
||||
|
@ -133,10 +133,11 @@ impl<T> HashSet<T, RandomState> {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
/// Creates an empty `HashSet` with the specified capacity.
|
||||
/// Creates an empty `HashSet` with at least the specified capacity.
|
||||
///
|
||||
/// The hash set will be able to hold at least `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the hash set will not allocate.
|
||||
/// reallocating. This method is allowed to allocate for more elements than
|
||||
/// `capacity`. If `capacity` is 0, the hash set will not allocate.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
@ -379,11 +380,12 @@ impl<T, S> HashSet<T, S> {
|
||||
HashSet { base: base::HashSet::with_hasher(hasher) }
|
||||
}
|
||||
|
||||
/// Creates an empty `HashSet` with the specified capacity, using
|
||||
/// Creates an empty `HashSet` with at least the specified capacity, using
|
||||
/// `hasher` to hash the keys.
|
||||
///
|
||||
/// The hash set will be able to hold at least `capacity` elements without
|
||||
/// reallocating. If `capacity` is 0, the hash set will not allocate.
|
||||
/// reallocating. This method is allowed to allocate for more elements than
|
||||
/// `capacity`. If `capacity` is 0, the hash set will not allocate.
|
||||
///
|
||||
/// Warning: `hasher` is normally randomly generated, and
|
||||
/// is designed to allow `HashSet`s to be resistant to attacks that
|
||||
@ -434,8 +436,10 @@ where
|
||||
S: BuildHasher,
|
||||
{
|
||||
/// Reserves capacity for at least `additional` more elements to be inserted
|
||||
/// in the `HashSet`. The collection may reserve more space to avoid
|
||||
/// frequent reallocations.
|
||||
/// in the `HashSet`. The collection may reserve more space to speculatively
|
||||
/// avoid 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 +460,11 @@ where
|
||||
}
|
||||
|
||||
/// Tries to reserve capacity for at least `additional` more elements to be inserted
|
||||
/// in the given `HashSet<K, V>`. The collection may reserve more space to avoid
|
||||
/// frequent reallocations.
|
||||
/// in the `HashSet`. The collection may reserve more space to speculatively
|
||||
/// avoid frequent reallocations. After calling `reserve`,
|
||||
/// capacity will be greater than or equal to `self.len() + additional` if
|
||||
/// it returns `Ok(())`.
|
||||
/// Does nothing if capacity is already sufficient.
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
@ -469,7 +476,7 @@ where
|
||||
/// ```
|
||||
/// use std::collections::HashSet;
|
||||
/// let mut set: HashSet<i32> = HashSet::new();
|
||||
/// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?");
|
||||
/// set.try_reserve(10).expect("why is the test harness OOMing on a handful of bytes?");
|
||||
/// ```
|
||||
#[inline]
|
||||
#[stable(feature = "try_reserve", since = "1.57.0")]
|
||||
|
@ -196,10 +196,11 @@ impl OsString {
|
||||
self.inner.push_slice(&s.as_ref().inner)
|
||||
}
|
||||
|
||||
/// Creates a new `OsString` with the given capacity.
|
||||
/// Creates a new `OsString` with at least the given capacity.
|
||||
///
|
||||
/// The string will be able to hold exactly `capacity` length units of other
|
||||
/// OS strings without reallocating. If `capacity` is 0, the string will not
|
||||
/// The string will be able to hold at least `capacity` length units of other
|
||||
/// OS strings without reallocating. This method is allowed to allocate for
|
||||
/// more units than `capacity`. If `capacity` is 0, the string will not
|
||||
/// allocate.
|
||||
///
|
||||
/// See the main `OsString` documentation information about encoding and capacity units.
|
||||
@ -263,9 +264,10 @@ impl OsString {
|
||||
}
|
||||
|
||||
/// Reserves capacity for at least `additional` more capacity to be inserted
|
||||
/// in the given `OsString`.
|
||||
/// in the given `OsString`. Does nothing if the capacity is
|
||||
/// already sufficient.
|
||||
///
|
||||
/// The collection may reserve more space to avoid frequent reallocations.
|
||||
/// The collection may reserve more space to speculatively avoid frequent reallocations.
|
||||
///
|
||||
/// See the main `OsString` documentation information about encoding and capacity units.
|
||||
///
|
||||
@ -285,10 +287,10 @@ impl OsString {
|
||||
}
|
||||
|
||||
/// Tries to reserve capacity for at least `additional` more length units
|
||||
/// in the given `OsString`. The string may reserve more space to avoid
|
||||
/// in the given `OsString`. The string 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.
|
||||
///
|
||||
/// See the main `OsString` documentation information about encoding and capacity units.
|
||||
///
|
||||
@ -322,7 +324,7 @@ impl OsString {
|
||||
self.inner.try_reserve(additional)
|
||||
}
|
||||
|
||||
/// Reserves the minimum capacity for exactly `additional` more capacity to
|
||||
/// Reserves the minimum capacity for at least `additional` more capacity to
|
||||
/// be inserted in the given `OsString`. Does nothing if the capacity is
|
||||
/// already sufficient.
|
||||
///
|
||||
@ -349,7 +351,7 @@ impl OsString {
|
||||
self.inner.reserve_exact(additional)
|
||||
}
|
||||
|
||||
/// Tries to reserve the minimum capacity for exactly `additional`
|
||||
/// Tries to reserve the minimum capacity for at least `additional`
|
||||
/// more length units in the given `OsString`. After calling
|
||||
/// `try_reserve_exact`, capacity will be greater than or equal to
|
||||
/// `self.len() + additional` if it returns `Ok(())`.
|
||||
|
@ -97,11 +97,11 @@ impl<W: Write> BufWriter<W> {
|
||||
BufWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
|
||||
}
|
||||
|
||||
/// Creates a new `BufWriter<W>` with the specified buffer capacity.
|
||||
/// Creates a new `BufWriter<W>` with at least the specified buffer capacity.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Creating a buffer with a buffer of a hundred bytes.
|
||||
/// Creating a buffer with a buffer of at least a hundred bytes.
|
||||
///
|
||||
/// ```no_run
|
||||
/// use std::io::BufWriter;
|
||||
|
@ -89,8 +89,8 @@ impl<W: Write> LineWriter<W> {
|
||||
LineWriter::with_capacity(1024, inner)
|
||||
}
|
||||
|
||||
/// Creates a new `LineWriter` with a specified capacity for the internal
|
||||
/// buffer.
|
||||
/// Creates a new `LineWriter` with at least the specified capacity for the
|
||||
/// internal buffer.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
Loading…
Reference in New Issue
Block a user