@@ -9,7 +9,7 @@ use core::ptr::{NonNull, Unique};
9
9
use core:: slice;
10
10
11
11
use crate :: alloc:: {
12
- handle_alloc_error, AllocErr ,
12
+ handle_alloc_error,
13
13
AllocInit :: { self , * } ,
14
14
AllocRef , Global , Layout ,
15
15
ReallocPlacement :: { self , * } ,
@@ -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,64 +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, MayMove )
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
- /// Attempts to ensure that the buffer contains at least enough space to hold
312
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already have
313
- /// enough capacity, will reallocate in place enough space plus comfortable slack
314
- /// space to get amortized `O(1)` behavior. Will limit this behaviour
315
- /// if it would needlessly cause itself to panic .
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 .
316
312
///
317
- /// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
318
- /// the requested space. This is not really unsafe, but the unsafe
319
- /// code *you* write that relies on the behavior of this function may break.
320
- ///
321
- /// Returns `true` if the reallocation attempt has succeeded.
322
- ///
323
- /// # Panics
324
- ///
325
- /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
326
- /// * Panics on 32-bit platforms if the requested capacity exceeds
327
- /// `isize::MAX` bytes.
328
- pub fn reserve_in_place ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
329
- // This is more readable than putting this in one line:
330
- // `!self.needs_to_grow(...) || self.grow(...).is_ok()`
331
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
332
- self . grow_amortized ( used_capacity, needed_extra_capacity, InPlace ) . is_ok ( )
333
- } else {
334
- true
335
- }
336
- }
337
-
338
- /// Ensures that the buffer contains at least enough space to hold
339
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
340
- /// will reallocate the minimum possible amount of memory necessary.
341
- /// Generally this will be exactly the amount of memory necessary,
342
- /// but in principle the allocator is free to give back more than what
343
- /// we asked for.
344
- ///
345
- /// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
346
- /// the requested space. This is not really unsafe, but the unsafe
347
- /// 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.
348
316
///
349
317
/// # Panics
350
318
///
@@ -355,8 +323,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
355
323
/// # Aborts
356
324
///
357
325
/// Aborts on OOM.
358
- pub fn reserve_exact ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
359
- 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 ) {
360
328
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
361
329
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
362
330
Ok ( ( ) ) => { /* yay */ }
@@ -366,14 +334,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
366
334
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
367
335
pub fn try_reserve_exact (
368
336
& mut self ,
369
- used_capacity : usize ,
370
- needed_extra_capacity : usize ,
337
+ len : usize ,
338
+ additional : usize ,
371
339
) -> Result < ( ) , TryReserveError > {
372
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
373
- self . grow_exact ( used_capacity, needed_extra_capacity)
374
- } else {
375
- Ok ( ( ) )
376
- }
340
+ if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
377
341
}
378
342
379
343
/// Shrinks the allocation down to the specified amount. If the given amount
@@ -398,8 +362,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
398
362
impl < T , A : AllocRef > RawVec < T , A > {
399
363
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
400
364
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
401
- fn needs_to_grow ( & self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
402
- 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 )
403
367
}
404
368
405
369
fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -419,14 +383,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
419
383
// so that all of the code that depends on `T` is within it, while as much
420
384
// of the code that doesn't depend on `T` as possible is in functions that
421
385
// are non-generic over `T`.
422
- fn grow_amortized (
423
- & mut self ,
424
- used_capacity : usize ,
425
- needed_extra_capacity : usize ,
426
- placement : ReallocPlacement ,
427
- ) -> Result < ( ) , TryReserveError > {
386
+ fn grow_amortized ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
428
387
// This is ensured by the calling contexts.
429
- debug_assert ! ( needed_extra_capacity > 0 ) ;
388
+ debug_assert ! ( additional > 0 ) ;
430
389
431
390
if mem:: size_of :: < T > ( ) == 0 {
432
391
// Since we return a capacity of `usize::MAX` when `elem_size` is
@@ -435,8 +394,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
435
394
}
436
395
437
396
// Nothing we can really do about these checks, sadly.
438
- let required_cap =
439
- used_capacity. checked_add ( needed_extra_capacity) . ok_or ( CapacityOverflow ) ?;
397
+ let required_cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
440
398
441
399
// This guarantees exponential growth. The doubling cannot overflow
442
400
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -461,30 +419,26 @@ impl<T, A: AllocRef> RawVec<T, A> {
461
419
let new_layout = Layout :: array :: < T > ( cap) ;
462
420
463
421
// `finish_grow` is non-generic over `T`.
464
- let memory = finish_grow ( new_layout, placement , self . current_memory ( ) , & mut self . alloc ) ?;
422
+ let memory = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
465
423
self . set_memory ( memory) ;
466
424
Ok ( ( ) )
467
425
}
468
426
469
427
// The constraints on this method are much the same as those on
470
428
// `grow_amortized`, but this method is usually instantiated less often so
471
429
// it's less critical.
472
- fn grow_exact (
473
- & mut self ,
474
- used_capacity : usize ,
475
- needed_extra_capacity : usize ,
476
- ) -> Result < ( ) , TryReserveError > {
430
+ fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
477
431
if mem:: size_of :: < T > ( ) == 0 {
478
432
// Since we return a capacity of `usize::MAX` when the type size is
479
433
// 0, getting to here necessarily means the `RawVec` is overfull.
480
434
return Err ( CapacityOverflow ) ;
481
435
}
482
436
483
- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
437
+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
484
438
let new_layout = Layout :: array :: < T > ( cap) ;
485
439
486
440
// `finish_grow` is non-generic over `T`.
487
- let memory = finish_grow ( new_layout, MayMove , self . current_memory ( ) , & mut self . alloc ) ?;
441
+ let memory = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
488
442
self . set_memory ( memory) ;
489
443
Ok ( ( ) )
490
444
}
@@ -518,7 +472,6 @@ impl<T, A: AllocRef> RawVec<T, A> {
518
472
// much smaller than the number of `T` types.)
519
473
fn finish_grow < A > (
520
474
new_layout : Result < Layout , LayoutErr > ,
521
- placement : ReallocPlacement ,
522
475
current_memory : Option < ( NonNull < u8 > , Layout ) > ,
523
476
alloc : & mut A ,
524
477
) -> Result < MemoryBlock , TryReserveError >
@@ -532,12 +485,9 @@ where
532
485
533
486
let memory = if let Some ( ( ptr, old_layout) ) = current_memory {
534
487
debug_assert_eq ! ( old_layout. align( ) , new_layout. align( ) ) ;
535
- unsafe { alloc. grow ( ptr, old_layout, new_layout. size ( ) , placement , Uninitialized ) }
488
+ unsafe { alloc. grow ( ptr, old_layout, new_layout. size ( ) , MayMove , Uninitialized ) }
536
489
} else {
537
- match placement {
538
- MayMove => alloc. alloc ( new_layout, Uninitialized ) ,
539
- InPlace => Err ( AllocErr ) ,
540
- }
490
+ alloc. alloc ( new_layout, Uninitialized )
541
491
}
542
492
. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?;
543
493
0 commit comments