@@ -9,7 +9,7 @@ use core::ptr::{NonNull, Unique};
99use core:: slice;
1010
1111use crate :: alloc:: {
12- handle_alloc_error, AllocErr ,
12+ handle_alloc_error,
1313 AllocInit :: { self , * } ,
1414 AllocRef , Global , Layout ,
1515 ReallocPlacement :: { self , * } ,
@@ -211,13 +211,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
211211 }
212212 }
213213
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.
219219 ///
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
221221 /// the requested space. This is not really unsafe, but the unsafe
222222 /// code *you* write that relies on the behavior of this function may break.
223223 ///
@@ -263,64 +263,32 @@ impl<T, A: AllocRef> RawVec<T, A> {
263263 /// # vector.push_all(&[1, 3, 5, 7, 9]);
264264 /// # }
265265 /// ```
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 ) {
268268 Err ( CapacityOverflow ) => capacity_overflow ( ) ,
269269 Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
270270 Ok ( ( ) ) => { /* yay */ }
271271 }
272272 }
273273
274274 /// 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)
282278 } else {
283279 Ok ( ( ) )
284280 }
285281 }
286282
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 .
292288 ///
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.
324292 ///
325293 /// # Panics
326294 ///
@@ -331,8 +299,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
331299 /// # Aborts
332300 ///
333301 /// 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 ) {
336304 Err ( CapacityOverflow ) => capacity_overflow ( ) ,
337305 Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
338306 Ok ( ( ) ) => { /* yay */ }
@@ -342,14 +310,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
342310 /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
343311 pub fn try_reserve_exact (
344312 & mut self ,
345- used_capacity : usize ,
346- needed_extra_capacity : usize ,
313+ len : usize ,
314+ additional : usize ,
347315 ) -> 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 ( ( ) ) }
353317 }
354318
355319 /// Shrinks the allocation down to the specified amount. If the given amount
@@ -374,8 +338,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
374338impl < T , A : AllocRef > RawVec < T , A > {
375339 /// Returns if the buffer needs to grow to fulfill the needed extra capacity.
376340 /// 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 )
379343 }
380344
381345 fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -395,14 +359,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
395359 // so that all of the code that depends on `T` is within it, while as much
396360 // of the code that doesn't depend on `T` as possible is in functions that
397361 // 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 > {
404363 // This is ensured by the calling contexts.
405- debug_assert ! ( needed_extra_capacity > 0 ) ;
364+ debug_assert ! ( additional > 0 ) ;
406365
407366 if mem:: size_of :: < T > ( ) == 0 {
408367 // Since we return a capacity of `usize::MAX` when `elem_size` is
@@ -411,8 +370,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
411370 }
412371
413372 // 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 ) ?;
416374
417375 // This guarantees exponential growth. The doubling cannot overflow
418376 // because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -437,30 +395,26 @@ impl<T, A: AllocRef> RawVec<T, A> {
437395 let new_layout = Layout :: array :: < T > ( cap) ;
438396
439397 // `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 ) ?;
441399 self . set_memory ( memory) ;
442400 Ok ( ( ) )
443401 }
444402
445403 // The constraints on this method are much the same as those on
446404 // `grow_amortized`, but this method is usually instantiated less often so
447405 // 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 > {
453407 if mem:: size_of :: < T > ( ) == 0 {
454408 // Since we return a capacity of `usize::MAX` when the type size is
455409 // 0, getting to here necessarily means the `RawVec` is overfull.
456410 return Err ( CapacityOverflow ) ;
457411 }
458412
459- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
413+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
460414 let new_layout = Layout :: array :: < T > ( cap) ;
461415
462416 // `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 ) ?;
464418 self . set_memory ( memory) ;
465419 Ok ( ( ) )
466420 }
@@ -494,7 +448,6 @@ impl<T, A: AllocRef> RawVec<T, A> {
494448// much smaller than the number of `T` types.)
495449fn finish_grow < A > (
496450 new_layout : Result < Layout , LayoutErr > ,
497- placement : ReallocPlacement ,
498451 current_memory : Option < ( NonNull < u8 > , Layout ) > ,
499452 alloc : & mut A ,
500453) -> Result < MemoryBlock , TryReserveError >
@@ -508,12 +461,9 @@ where
508461
509462 let memory = if let Some ( ( ptr, old_layout) ) = current_memory {
510463 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 ) }
512465 } else {
513- match placement {
514- MayMove => alloc. alloc ( new_layout, Uninitialized ) ,
515- InPlace => Err ( AllocErr ) ,
516- }
466+ alloc. alloc ( new_layout, Uninitialized )
517467 }
518468 . map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?;
519469
0 commit comments