Skip to content

Commit c0991ae

Browse files
committed
Don't panic in Vec::shrink_to_fit
1 parent dbabbb7 commit c0991ae

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

alloc/src/vec.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,10 @@ impl<T> Vec<T> {
622622
/// ```
623623
#[stable(feature = "rust1", since = "1.0.0")]
624624
pub fn shrink_to_fit(&mut self) {
625-
if self.capacity() != self.len {
625+
// The capacity is never less than the length, and there's nothing to do when
626+
// they are equal, so we can avoid the panic case in `RawVec::shrink_to_fit`
627+
// by only calling it with a greater capacity.
628+
if self.capacity() > self.len {
626629
self.buf.shrink_to_fit(self.len);
627630
}
628631
}

0 commit comments

Comments
 (0)