@@ -235,13 +235,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
235
235
}
236
236
}
237
237
238
- /// Ensures that the buffer contains at least enough space to hold
239
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already have
240
- /// enough capacity, will reallocate enough space plus comfortable slack
241
- /// space to get amortized `O(1)` behavior. Will limit this behavior
242
- /// if it would needlessly cause itself to panic.
238
+ /// Ensures that the buffer contains at least enough space to hold `len +
239
+ /// additional` elements. If it doesn't already have enough capacity, will
240
+ /// reallocate enough space plus comfortable slack space to get amortized
241
+ /// `O(1)` behavior. Will limit this behavior if it would needlessly cause
242
+ /// itself to panic.
243
243
///
244
- /// If `used_capacity ` exceeds `self.capacity()`, this may fail to actually allocate
244
+ /// If `len ` exceeds `self.capacity()`, this may fail to actually allocate
245
245
/// the requested space. This is not really unsafe, but the unsafe
246
246
/// code *you* write that relies on the behavior of this function may break.
247
247
///
@@ -287,37 +287,32 @@ impl<T, A: AllocRef> RawVec<T, A> {
287
287
/// # vector.push_all(&[1, 3, 5, 7, 9]);
288
288
/// # }
289
289
/// ```
290
- pub fn reserve ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
291
- match self . try_reserve ( used_capacity , needed_extra_capacity ) {
290
+ pub fn reserve ( & mut self , len : usize , additional : usize ) {
291
+ match self . try_reserve ( len , additional ) {
292
292
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
293
293
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
294
294
Ok ( ( ) ) => { /* yay */ }
295
295
}
296
296
}
297
297
298
298
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
299
- pub fn try_reserve (
300
- & mut self ,
301
- used_capacity : usize ,
302
- needed_extra_capacity : usize ,
303
- ) -> Result < ( ) , TryReserveError > {
304
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
305
- self . grow_amortized ( used_capacity, needed_extra_capacity)
299
+ pub fn try_reserve ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
300
+ if self . needs_to_grow ( len, additional) {
301
+ self . grow_amortized ( len, additional)
306
302
} else {
307
303
Ok ( ( ) )
308
304
}
309
305
}
310
306
311
- /// Ensures that the buffer contains at least enough space to hold
312
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
313
- /// will reallocate the minimum possible amount of memory necessary.
314
- /// Generally this will be exactly the amount of memory necessary,
315
- /// but in principle the allocator is free to give back more than what
316
- /// we asked for.
307
+ /// Ensures that the buffer contains at least enough space to hold `len +
308
+ /// additional` elements. If it doesn't already, will reallocate the
309
+ /// minimum possible amount of memory necessary. Generally this will be
310
+ /// exactly the amount of memory necessary, but in principle the allocator
311
+ /// is free to give back more than we asked for.
317
312
///
318
- /// If `used_capacity ` exceeds `self.capacity()`, this may fail to actually allocate
319
- /// the requested space. This is not really unsafe, but the unsafe
320
- /// code *you* write that relies on the behavior of this function may break.
313
+ /// If `len ` exceeds `self.capacity()`, this may fail to actually allocate
314
+ /// the requested space. This is not really unsafe, but the unsafe code
315
+ /// *you* write that relies on the behavior of this function may break.
321
316
///
322
317
/// # Panics
323
318
///
@@ -328,8 +323,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
328
323
/// # Aborts
329
324
///
330
325
/// Aborts on OOM.
331
- pub fn reserve_exact ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
332
- match self . try_reserve_exact ( used_capacity , needed_extra_capacity ) {
326
+ pub fn reserve_exact ( & mut self , len : usize , additional : usize ) {
327
+ match self . try_reserve_exact ( len , additional ) {
333
328
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
334
329
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
335
330
Ok ( ( ) ) => { /* yay */ }
@@ -339,14 +334,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
339
334
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
340
335
pub fn try_reserve_exact (
341
336
& mut self ,
342
- used_capacity : usize ,
343
- needed_extra_capacity : usize ,
337
+ len : usize ,
338
+ additional : usize ,
344
339
) -> Result < ( ) , TryReserveError > {
345
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
346
- self . grow_exact ( used_capacity, needed_extra_capacity)
347
- } else {
348
- Ok ( ( ) )
349
- }
340
+ if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
350
341
}
351
342
352
343
/// Shrinks the allocation down to the specified amount. If the given amount
@@ -371,8 +362,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
371
362
impl < T , A : AllocRef > RawVec < T , A > {
372
363
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
373
364
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
374
- fn needs_to_grow ( & self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
375
- needed_extra_capacity > self . capacity ( ) . wrapping_sub ( used_capacity )
365
+ fn needs_to_grow ( & self , len : usize , additional : usize ) -> bool {
366
+ additional > self . capacity ( ) . wrapping_sub ( len )
376
367
}
377
368
378
369
fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -392,22 +383,18 @@ impl<T, A: AllocRef> RawVec<T, A> {
392
383
// so that all of the code that depends on `T` is within it, while as much
393
384
// of the code that doesn't depend on `T` as possible is in functions that
394
385
// are non-generic over `T`.
395
- fn grow_amortized (
396
- & mut self ,
397
- used_capacity : usize ,
398
- needed_extra_capacity : usize ,
399
- ) -> Result < ( ) , TryReserveError > {
386
+ fn grow_amortized ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
400
387
// This is ensured by the calling contexts.
401
- debug_assert ! ( needed_extra_capacity > 0 ) ;
388
+ debug_assert ! ( additional > 0 ) ;
389
+
402
390
if mem:: size_of :: < T > ( ) == 0 {
403
391
// Since we return a capacity of `usize::MAX` when `elem_size` is
404
392
// 0, getting to here necessarily means the `RawVec` is overfull.
405
393
return Err ( CapacityOverflow ) ;
406
394
}
407
395
408
396
// Nothing we can really do about these checks, sadly.
409
- let required_cap =
410
- used_capacity. checked_add ( needed_extra_capacity) . ok_or ( CapacityOverflow ) ?;
397
+ let required_cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
411
398
412
399
// This guarantees exponential growth. The doubling cannot overflow
413
400
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -440,18 +427,14 @@ impl<T, A: AllocRef> RawVec<T, A> {
440
427
// The constraints on this method are much the same as those on
441
428
// `grow_amortized`, but this method is usually instantiated less often so
442
429
// it's less critical.
443
- fn grow_exact (
444
- & mut self ,
445
- used_capacity : usize ,
446
- needed_extra_capacity : usize ,
447
- ) -> Result < ( ) , TryReserveError > {
430
+ fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
448
431
if mem:: size_of :: < T > ( ) == 0 {
449
432
// Since we return a capacity of `usize::MAX` when the type size is
450
433
// 0, getting to here necessarily means the `RawVec` is overfull.
451
434
return Err ( CapacityOverflow ) ;
452
435
}
453
436
454
- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
437
+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
455
438
let new_layout = Layout :: array :: < T > ( cap) ;
456
439
457
440
// `finish_grow` is non-generic over `T`.
0 commit comments