Remove len argument from RawVec::reserve_for_push because it's always equal to capacity. Also make Vec::insert use reserve_for_push.

This commit is contained in:
Cai Bear 2024-03-23 21:27:59 -07:00
parent ba527200cc
commit 18d390883e
3 changed files with 6 additions and 7 deletions

View File

@ -2086,10 +2086,9 @@ impl<T, A: Allocator> VecDeque<T, A> {
// Extend or possibly remove this assertion when valid use-cases for growing the // Extend or possibly remove this assertion when valid use-cases for growing the
// buffer without it being full emerge // buffer without it being full emerge
debug_assert!(self.is_full()); debug_assert!(self.is_full());
let old_cap = self.capacity(); self.buf.reserve_for_push();
self.buf.reserve_for_push(old_cap);
unsafe { unsafe {
self.handle_capacity_increase(old_cap); self.handle_capacity_increase(self.capacity());
} }
debug_assert!(!self.is_full()); debug_assert!(!self.is_full());
} }

View File

@ -349,8 +349,8 @@ impl<T, A: Allocator> RawVec<T, A> {
/// oft-instantiated `Vec::push()`, which does its own capacity check. /// oft-instantiated `Vec::push()`, which does its own capacity check.
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline(never)] #[inline(never)]
pub fn reserve_for_push(&mut self, len: usize) { pub fn reserve_for_push(&mut self) {
if let Err(err) = self.grow_amortized(len, 1) { if let Err(err) = self.grow_amortized(self.cap.0, 1) {
handle_error(err); handle_error(err);
} }
} }

View File

@ -1547,7 +1547,7 @@ impl<T, A: Allocator> Vec<T, A> {
// space for the new element // space for the new element
if len == self.buf.capacity() { if len == self.buf.capacity() {
self.reserve(1); self.buf.reserve_for_push();
} }
unsafe { unsafe {
@ -1967,7 +1967,7 @@ impl<T, A: Allocator> Vec<T, A> {
// This will panic or abort if we would allocate > isize::MAX bytes // This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types. // or if the length increment would overflow for zero-sized types.
if self.len == self.buf.capacity() { if self.len == self.buf.capacity() {
self.buf.reserve_for_push(self.len); self.buf.reserve_for_push();
} }
unsafe { unsafe {
let end = self.as_mut_ptr().add(self.len); let end = self.as_mut_ptr().add(self.len);