@@ -211,13 +211,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
211
211
}
212
212
}
213
213
214
- /// Ensures that the buffer contains at least enough space to hold
215
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already have
216
- /// enough capacity, will reallocate enough space plus comfortable slack
217
- /// space to get amortized `O(1)` behavior. Will limit this behavior
218
- /// if it would needlessly cause itself to panic.
214
+ /// Ensures that the buffer contains at least enough space to hold `len +
215
+ /// additional` elements. If it doesn't already have enough capacity, will
216
+ /// reallocate enough space plus comfortable slack space to get amortized
217
+ /// `O(1)` behavior. Will limit this behavior if it would needlessly cause
218
+ /// itself to panic.
219
219
///
220
- /// If `used_capacity ` exceeds `self.capacity()`, this may fail to actually allocate
220
+ /// If `len ` exceeds `self.capacity()`, this may fail to actually allocate
221
221
/// the requested space. This is not really unsafe, but the unsafe
222
222
/// code *you* write that relies on the behavior of this function may break.
223
223
///
@@ -263,37 +263,32 @@ impl<T, A: AllocRef> RawVec<T, A> {
263
263
/// # vector.push_all(&[1, 3, 5, 7, 9]);
264
264
/// # }
265
265
/// ```
266
- pub fn reserve ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
267
- match self . try_reserve ( used_capacity , needed_extra_capacity ) {
266
+ pub fn reserve ( & mut self , len : usize , additional : usize ) {
267
+ match self . try_reserve ( len , additional ) {
268
268
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
269
269
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
270
270
Ok ( ( ) ) => { /* yay */ }
271
271
}
272
272
}
273
273
274
274
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
275
- pub fn try_reserve (
276
- & mut self ,
277
- used_capacity : usize ,
278
- needed_extra_capacity : usize ,
279
- ) -> Result < ( ) , TryReserveError > {
280
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
281
- self . grow_amortized ( used_capacity, needed_extra_capacity)
275
+ pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
276
+ if self . needs_to_grow ( len, additional) {
277
+ self . grow_amortized ( len, additional)
282
278
} else {
283
279
Ok ( ( ) )
284
280
}
285
281
}
286
282
287
- /// Ensures that the buffer contains at least enough space to hold
288
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
289
- /// will reallocate the minimum possible amount of memory necessary.
290
- /// Generally this will be exactly the amount of memory necessary,
291
- /// but in principle the allocator is free to give back more than what
292
- /// we asked for.
283
+ /// Ensures that the buffer contains at least enough space to hold `len +
284
+ /// additional` elements. If it doesn't already, will reallocate the
285
+ /// minimum possible amount of memory necessary. Generally this will be
286
+ /// exactly the amount of memory necessary, but in principle the allocator
287
+ /// is free to give back more than we asked for.
293
288
///
294
- /// If `used_capacity ` exceeds `self.capacity()`, this may fail to actually allocate
295
- /// the requested space. This is not really unsafe, but the unsafe
296
- /// code *you* write that relies on the behavior of this function may break.
289
+ /// If `len ` exceeds `self.capacity()`, this may fail to actually allocate
290
+ /// the requested space. This is not really unsafe, but the unsafe code
291
+ /// *you* write that relies on the behavior of this function may break.
297
292
///
298
293
/// # Panics
299
294
///
@@ -304,8 +299,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
304
299
/// # Aborts
305
300
///
306
301
/// Aborts on OOM.
307
- pub fn reserve_exact ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
308
- match self . try_reserve_exact ( used_capacity , needed_extra_capacity ) {
302
+ pub fn reserve_exact ( & mut self , len : usize , additional : usize ) {
303
+ match self . try_reserve_exact ( len , additional ) {
309
304
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
310
305
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
311
306
Ok ( ( ) ) => { /* yay */ }
@@ -315,14 +310,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
315
310
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
316
311
pub fn try_reserve_exact (
317
312
& mut self ,
318
- used_capacity : usize ,
319
- needed_extra_capacity : usize ,
313
+ len : usize ,
314
+ additional : usize ,
320
315
) -> Result < ( ) , TryReserveError > {
321
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
322
- self . grow_exact ( used_capacity, needed_extra_capacity)
323
- } else {
324
- Ok ( ( ) )
325
- }
316
+ if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
326
317
}
327
318
328
319
/// Shrinks the allocation down to the specified amount. If the given amount
@@ -347,8 +338,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
347
338
impl < T , A : AllocRef > RawVec < T , A > {
348
339
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
349
340
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
350
- fn needs_to_grow ( & self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
351
- needed_extra_capacity > self . capacity ( ) . wrapping_sub ( used_capacity )
341
+ fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
342
+ additional > self . capacity ( ) . wrapping_sub ( len )
352
343
}
353
344
354
345
fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -368,22 +359,18 @@ impl<T, A: AllocRef> RawVec<T, A> {
368
359
// so that all of the code that depends on `T` is within it, while as much
369
360
// of the code that doesn't depend on `T` as possible is in functions that
370
361
// are non-generic over `T`.
371
- fn grow_amortized (
372
- & mut self ,
373
- used_capacity : usize ,
374
- needed_extra_capacity : usize ,
375
- ) -> Result < ( ) , TryReserveError > {
362
+ fn grow_amortized ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
376
363
// This is ensured by the calling contexts.
377
- debug_assert ! ( needed_extra_capacity > 0 ) ;
364
+ debug_assert ! ( additional > 0 ) ;
365
+
378
366
if mem:: size_of :: < T > ( ) == 0 {
379
367
// Since we return a capacity of `usize::MAX` when `elem_size` is
380
368
// 0, getting to here necessarily means the `RawVec` is overfull.
381
369
return Err ( CapacityOverflow ) ;
382
370
}
383
371
384
372
// Nothing we can really do about these checks, sadly.
385
- let required_cap =
386
- used_capacity. checked_add ( needed_extra_capacity) . ok_or ( CapacityOverflow ) ?;
373
+ let required_cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
387
374
388
375
// This guarantees exponential growth. The doubling cannot overflow
389
376
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -416,18 +403,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
416
403
// The constraints on this method are much the same as those on
417
404
// `grow_amortized`, but this method is usually instantiated less often so
418
405
// it's less critical.
419
- fn grow_exact (
420
- & mut self ,
421
- used_capacity : usize ,
422
- needed_extra_capacity : usize ,
423
- ) -> Result < ( ) , TryReserveError > {
406
+ fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
424
407
if mem:: size_of :: < T > ( ) == 0 {
425
408
// Since we return a capacity of `usize::MAX` when the type size is
426
409
// 0, getting to here necessarily means the `RawVec` is overfull.
427
410
return Err ( CapacityOverflow ) ;
428
411
}
429
412
430
- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
413
+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
431
414
let new_layout = Layout :: array :: < T > ( cap) ;
432
415
433
416
// `finish_grow` is non-generic over `T`.
0 commit comments