@@ -185,20 +185,19 @@ where
185
185
self . len
186
186
}
187
187
188
- /// Forcefully sets `self.len` to `new_len `.
188
+ /// Increments `self.len` by `additional `.
189
189
///
190
190
/// # Safety
191
191
///
192
- /// - `new_len` must be less than or equal to [`Self::capacity`].
193
- /// - If `new_len` is greater than `self.len`, all elements within the interval
194
- /// [`self.len`,`new_len`) must be initialized.
192
+ /// - `additional` must be less than or equal to `self.capacity - self.len`.
193
+ /// - All elements within the interval [`self.len`,`self.len + additional`) must be initialized.
195
194
#[ inline]
196
- pub unsafe fn set_len ( & mut self , new_len : usize ) {
197
- debug_assert ! ( new_len <= self . capacity ( ) ) ;
198
-
199
- // INVARIANT: By the safety requirements of this method `new_len` represents the exact
200
- // number of elements stored within `self`.
201
- self . len = new_len ;
195
+ pub unsafe fn inc_len ( & mut self , additional : usize ) {
196
+ // Guaranteed by the type invariant to never underflow.
197
+ debug_assert ! ( additional <= self . capacity ( ) - self . len ( ) ) ;
198
+ // INVARIANT: By the safety requirements of this method this represents the exact number of
199
+ // elements stored within `self`.
200
+ self . len += additional ;
202
201
}
203
202
204
203
/// Decreases `self.len` by `count`.
@@ -317,7 +316,7 @@ where
317
316
// SAFETY: We just initialised the first spare entry, so it is safe to increase the length
318
317
// by 1. We also know that the new length is <= capacity because of the previous call to
319
318
// `reserve` above.
320
- unsafe { self . set_len ( self . len ( ) + 1 ) } ;
319
+ unsafe { self . inc_len ( 1 ) } ;
321
320
Ok ( ( ) )
322
321
}
323
322
@@ -521,7 +520,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
521
520
// SAFETY:
522
521
// - `self.len() + n < self.capacity()` due to the call to reserve above,
523
522
// - the loop and the line above initialized the next `n` elements.
524
- unsafe { self . set_len ( self . len ( ) + n) } ;
523
+ unsafe { self . inc_len ( n) } ;
525
524
526
525
Ok ( ( ) )
527
526
}
@@ -552,7 +551,7 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
552
551
// the length by the same number.
553
552
// - `self.len() + other.len() <= self.capacity()` is guaranteed by the preceding `reserve`
554
553
// call.
555
- unsafe { self . set_len ( self . len ( ) + other. len ( ) ) } ;
554
+ unsafe { self . inc_len ( other. len ( ) ) } ;
556
555
Ok ( ( ) )
557
556
}
558
557
0 commit comments