diff options
| author | Thom Wiggers <thom@thomwiggers.nl> | 2021-01-24 13:29:39 +0100 |
|---|---|---|
| committer | Thom Wiggers <thom@thomwiggers.nl> | 2021-01-26 19:25:37 +0100 |
| commit | d069c58e78891d453b0b17adfecd368694fd289f (patch) | |
| tree | 097bcb7a715f4b3e38faf9a1e004405bb92669fd /library/alloc | |
| parent | 85e355ea9bd86ac6580a5d422a65dbf689845808 (diff) | |
| download | rust-d069c58e78891d453b0b17adfecd368694fd289f.tar.gz rust-d069c58e78891d453b0b17adfecd368694fd289f.zip | |
shrink_to shouldn't panic on len greater than capacity
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/collections/binary_heap.rs | 3 | ||||
| -rw-r--r-- | library/alloc/src/collections/vec_deque/mod.rs | 10 | ||||
| -rw-r--r-- | library/alloc/src/string.rs | 3 | ||||
| -rw-r--r-- | library/alloc/src/vec/mod.rs | 12 |
4 files changed, 12 insertions, 16 deletions
diff --git a/library/alloc/src/collections/binary_heap.rs b/library/alloc/src/collections/binary_heap.rs index 3c515af71f5..8a36b2af765 100644 --- a/library/alloc/src/collections/binary_heap.rs +++ b/library/alloc/src/collections/binary_heap.rs @@ -870,8 +870,7 @@ impl<T> BinaryHeap<T> { /// The capacity will remain at least as large as both the length /// and the supplied value. /// - /// Panics if the current capacity is smaller than the supplied - /// minimum capacity. + /// If the current capacity is less than the lower limit, this is a no-op. /// /// # Examples /// diff --git a/library/alloc/src/collections/vec_deque/mod.rs b/library/alloc/src/collections/vec_deque/mod.rs index 6f9133e2811..eb899468193 100644 --- a/library/alloc/src/collections/vec_deque/mod.rs +++ b/library/alloc/src/collections/vec_deque/mod.rs @@ -761,8 +761,7 @@ impl<T> VecDeque<T> { /// The capacity will remain at least as large as both the length /// and the supplied value. /// - /// Panics if the current capacity is smaller than the supplied - /// minimum capacity. + /// If the current capacity is less than the lower limit, this is a no-op. /// /// # Examples /// @@ -780,10 +779,9 @@ impl<T> VecDeque<T> { /// ``` #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] pub fn shrink_to(&mut self, min_capacity: usize) { - assert!(self.capacity() >= min_capacity, "Tried to shrink to a larger capacity"); - - // +1 since the ringbuffer always leaves one space empty - // len + 1 can't overflow for an existing, well-formed ringbuffer. + let min_capacity = cmp::min(min_capacity, self.capacity()); + // We don't have to worry about an overflow as neither `self.len()` nor `self.capacity()` + // can ever be `usize::MAX`. +1 as the ringbuffer always leaves one space empty. let target_cap = cmp::max(cmp::max(min_capacity, self.len()) + 1, MINIMUM_CAPACITY + 1) .next_power_of_two(); diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index b1f860d6b64..9b0b480a7e9 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -1036,8 +1036,7 @@ impl String { /// The capacity will remain at least as large as both the length /// and the supplied value. /// - /// Panics if the current capacity is smaller than the supplied - /// minimum capacity. + /// If the current capacity is less than the lower limit, this is a no-op. /// /// # Examples /// diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b533ce79420..13fcf5207e0 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -321,7 +321,7 @@ mod spec_extend; /// ensures no unnecessary allocations or deallocations occur. Emptying a `Vec` /// and then filling it back up to the same [`len`] should incur no calls to /// the allocator. If you wish to free up unused memory, use -/// [`shrink_to_fit`]. +/// [`shrink_to_fit`] or [`shrink_to`]. /// /// [`push`] and [`insert`] will never (re)allocate if the reported capacity is /// sufficient. [`push`] and [`insert`] *will* (re)allocate if @@ -360,6 +360,7 @@ mod spec_extend; /// [`String`]: crate::string::String /// [`&str`]: type@str /// [`shrink_to_fit`]: Vec::shrink_to_fit +/// [`shrink_to`]: Vec::shrink_to /// [`capacity`]: Vec::capacity /// [`mem::size_of::<T>`]: core::mem::size_of /// [`len`]: Vec::len @@ -909,10 +910,7 @@ impl<T, A: Allocator> Vec<T, A> { /// The capacity will remain at least as large as both the length /// and the supplied value. /// - /// # Panics - /// - /// Panics if the current capacity is smaller than the supplied - /// minimum capacity. + /// If the current capacity is less than the lower limit, this is a no-op. /// /// # Examples /// @@ -929,7 +927,9 @@ impl<T, A: Allocator> Vec<T, A> { #[doc(alias = "realloc")] #[unstable(feature = "shrink_to", reason = "new API", issue = "56431")] pub fn shrink_to(&mut self, min_capacity: usize) { - self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); + if self.capacity() > min_capacity { + self.buf.shrink_to_fit(cmp::max(self.len, min_capacity)); + } } /// Converts the vector into [`Box<[T]>`][owned slice]. |
