@@ -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 , * } ,
@@ -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,64 +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, MayMove )
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
- /// Attempts to ensure that the buffer contains at least enough space to hold
288
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already have
289
- /// enough capacity, will reallocate in place enough space plus comfortable slack
290
- /// space to get amortized `O(1)` behavior. Will limit this behaviour
291
- /// if it would needlessly cause itself to panic .
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 .
292
288
///
293
- /// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
294
- /// the requested space. This is not really unsafe, but the unsafe
295
- /// code *you* write that relies on the behavior of this function may break.
296
- ///
297
- /// Returns `true` if the reallocation attempt has succeeded.
298
- ///
299
- /// # Panics
300
- ///
301
- /// * Panics if the requested capacity exceeds `usize::MAX` bytes.
302
- /// * Panics on 32-bit platforms if the requested capacity exceeds
303
- /// `isize::MAX` bytes.
304
- pub fn reserve_in_place ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
305
- // This is more readable than putting this in one line:
306
- // `!self.needs_to_grow(...) || self.grow(...).is_ok()`
307
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
308
- self . grow_amortized ( used_capacity, needed_extra_capacity, InPlace ) . is_ok ( )
309
- } else {
310
- true
311
- }
312
- }
313
-
314
- /// Ensures that the buffer contains at least enough space to hold
315
- /// `used_capacity + needed_extra_capacity` elements. If it doesn't already,
316
- /// will reallocate the minimum possible amount of memory necessary.
317
- /// Generally this will be exactly the amount of memory necessary,
318
- /// but in principle the allocator is free to give back more than what
319
- /// we asked for.
320
- ///
321
- /// If `used_capacity` exceeds `self.capacity()`, this may fail to actually allocate
322
- /// the requested space. This is not really unsafe, but the unsafe
323
- /// 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.
324
292
///
325
293
/// # Panics
326
294
///
@@ -331,8 +299,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
331
299
/// # Aborts
332
300
///
333
301
/// Aborts on OOM.
334
- pub fn reserve_exact ( & mut self , used_capacity : usize , needed_extra_capacity : usize ) {
335
- 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 ) {
336
304
Err ( CapacityOverflow ) => capacity_overflow ( ) ,
337
305
Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
338
306
Ok ( ( ) ) => { /* yay */ }
@@ -342,14 +310,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
342
310
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
343
311
pub fn try_reserve_exact (
344
312
& mut self ,
345
- used_capacity : usize ,
346
- needed_extra_capacity : usize ,
313
+ len : usize ,
314
+ additional : usize ,
347
315
) -> Result < ( ) , TryReserveError > {
348
- if self . needs_to_grow ( used_capacity, needed_extra_capacity) {
349
- self . grow_exact ( used_capacity, needed_extra_capacity)
350
- } else {
351
- Ok ( ( ) )
352
- }
316
+ if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
353
317
}
354
318
355
319
/// Shrinks the allocation down to the specified amount. If the given amount
@@ -374,8 +338,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
374
338
impl < T , A : AllocRef > RawVec < T , A > {
375
339
/// Returns if the buffer needs to grow to fulfill the needed extra capacity.
376
340
/// Mainly used to make inlining reserve-calls possible without inlining `grow`.
377
- fn needs_to_grow ( & self , used_capacity : usize , needed_extra_capacity : usize ) -> bool {
378
- 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 )
379
343
}
380
344
381
345
fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -395,14 +359,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
395
359
// so that all of the code that depends on `T` is within it, while as much
396
360
// of the code that doesn't depend on `T` as possible is in functions that
397
361
// are non-generic over `T`.
398
- fn grow_amortized (
399
- & mut self ,
400
- used_capacity : usize ,
401
- needed_extra_capacity : usize ,
402
- placement : ReallocPlacement ,
403
- ) -> Result < ( ) , TryReserveError > {
362
+ fn grow_amortized ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
404
363
// This is ensured by the calling contexts.
405
- debug_assert ! ( needed_extra_capacity > 0 ) ;
364
+ debug_assert ! ( additional > 0 ) ;
406
365
407
366
if mem:: size_of :: < T > ( ) == 0 {
408
367
// Since we return a capacity of `usize::MAX` when `elem_size` is
@@ -411,8 +370,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
411
370
}
412
371
413
372
// Nothing we can really do about these checks, sadly.
414
- let required_cap =
415
- used_capacity. checked_add ( needed_extra_capacity) . ok_or ( CapacityOverflow ) ?;
373
+ let required_cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
416
374
417
375
// This guarantees exponential growth. The doubling cannot overflow
418
376
// because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -437,30 +395,26 @@ impl<T, A: AllocRef> RawVec<T, A> {
437
395
let new_layout = Layout :: array :: < T > ( cap) ;
438
396
439
397
// `finish_grow` is non-generic over `T`.
440
- let memory = finish_grow ( new_layout, placement , self . current_memory ( ) , & mut self . alloc ) ?;
398
+ let memory = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
441
399
self . set_memory ( memory) ;
442
400
Ok ( ( ) )
443
401
}
444
402
445
403
// The constraints on this method are much the same as those on
446
404
// `grow_amortized`, but this method is usually instantiated less often so
447
405
// it's less critical.
448
- fn grow_exact (
449
- & mut self ,
450
- used_capacity : usize ,
451
- needed_extra_capacity : usize ,
452
- ) -> Result < ( ) , TryReserveError > {
406
+ fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
453
407
if mem:: size_of :: < T > ( ) == 0 {
454
408
// Since we return a capacity of `usize::MAX` when the type size is
455
409
// 0, getting to here necessarily means the `RawVec` is overfull.
456
410
return Err ( CapacityOverflow ) ;
457
411
}
458
412
459
- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
413
+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
460
414
let new_layout = Layout :: array :: < T > ( cap) ;
461
415
462
416
// `finish_grow` is non-generic over `T`.
463
- let memory = finish_grow ( new_layout, MayMove , self . current_memory ( ) , & mut self . alloc ) ?;
417
+ let memory = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
464
418
self . set_memory ( memory) ;
465
419
Ok ( ( ) )
466
420
}
@@ -494,7 +448,6 @@ impl<T, A: AllocRef> RawVec<T, A> {
494
448
// much smaller than the number of `T` types.)
495
449
fn finish_grow < A > (
496
450
new_layout : Result < Layout , LayoutErr > ,
497
- placement : ReallocPlacement ,
498
451
current_memory : Option < ( NonNull < u8 > , Layout ) > ,
499
452
alloc : & mut A ,
500
453
) -> Result < MemoryBlock , TryReserveError >
@@ -508,12 +461,9 @@ where
508
461
509
462
let memory = if let Some ( ( ptr, old_layout) ) = current_memory {
510
463
debug_assert_eq ! ( old_layout. align( ) , new_layout. align( ) ) ;
511
- unsafe { alloc. grow ( ptr, old_layout, new_layout. size ( ) , placement , Uninitialized ) }
464
+ unsafe { alloc. grow ( ptr, old_layout, new_layout. size ( ) , MayMove , Uninitialized ) }
512
465
} else {
513
- match placement {
514
- MayMove => alloc. alloc ( new_layout, Uninitialized ) ,
515
- InPlace => Err ( AllocErr ) ,
516
- }
466
+ alloc. alloc ( new_layout, Uninitialized )
517
467
}
518
468
. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?;
519
469
0 commit comments