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