@@ -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 , * } ,
@@ -235,13 +235,13 @@ impl<T, A: AllocRef> RawVec<T, A> {
235235 }
236236 }
237237
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.
243243 ///
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
245245 /// the requested space. This is not really unsafe, but the unsafe
246246 /// code *you* write that relies on the behavior of this function may break.
247247 ///
@@ -287,64 +287,32 @@ impl<T, A: AllocRef> RawVec<T, A> {
287287 /// # vector.push_all(&[1, 3, 5, 7, 9]);
288288 /// # }
289289 /// ```
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 ) {
292292 Err ( CapacityOverflow ) => capacity_overflow ( ) ,
293293 Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
294294 Ok ( ( ) ) => { /* yay */ }
295295 }
296296 }
297297
298298 /// 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)
306302 } else {
307303 Ok ( ( ) )
308304 }
309305 }
310306
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 .
316312 ///
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.
348316 ///
349317 /// # Panics
350318 ///
@@ -355,8 +323,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
355323 /// # Aborts
356324 ///
357325 /// 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 ) {
360328 Err ( CapacityOverflow ) => capacity_overflow ( ) ,
361329 Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
362330 Ok ( ( ) ) => { /* yay */ }
@@ -366,14 +334,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
366334 /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
367335 pub fn try_reserve_exact (
368336 & mut self ,
369- used_capacity : usize ,
370- needed_extra_capacity : usize ,
337+ len : usize ,
338+ additional : usize ,
371339 ) -> 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 ( ( ) ) }
377341 }
378342
379343 /// Shrinks the allocation down to the specified amount. If the given amount
@@ -398,8 +362,8 @@ impl<T, A: AllocRef> RawVec<T, A> {
398362impl < T , A : AllocRef > RawVec < T , A > {
399363 /// Returns if the buffer needs to grow to fulfill the needed extra capacity.
400364 /// 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 )
403367 }
404368
405369 fn capacity_from_bytes ( excess : usize ) -> usize {
@@ -419,14 +383,9 @@ impl<T, A: AllocRef> RawVec<T, A> {
419383 // so that all of the code that depends on `T` is within it, while as much
420384 // of the code that doesn't depend on `T` as possible is in functions that
421385 // 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 > {
428387 // This is ensured by the calling contexts.
429- debug_assert ! ( needed_extra_capacity > 0 ) ;
388+ debug_assert ! ( additional > 0 ) ;
430389
431390 if mem:: size_of :: < T > ( ) == 0 {
432391 // Since we return a capacity of `usize::MAX` when `elem_size` is
@@ -435,8 +394,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
435394 }
436395
437396 // 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 ) ?;
440398
441399 // This guarantees exponential growth. The doubling cannot overflow
442400 // because `cap <= isize::MAX` and the type of `cap` is `usize`.
@@ -461,30 +419,26 @@ impl<T, A: AllocRef> RawVec<T, A> {
461419 let new_layout = Layout :: array :: < T > ( cap) ;
462420
463421 // `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 ) ?;
465423 self . set_memory ( memory) ;
466424 Ok ( ( ) )
467425 }
468426
469427 // The constraints on this method are much the same as those on
470428 // `grow_amortized`, but this method is usually instantiated less often so
471429 // 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 > {
477431 if mem:: size_of :: < T > ( ) == 0 {
478432 // Since we return a capacity of `usize::MAX` when the type size is
479433 // 0, getting to here necessarily means the `RawVec` is overfull.
480434 return Err ( CapacityOverflow ) ;
481435 }
482436
483- let cap = used_capacity . checked_add ( needed_extra_capacity ) . ok_or ( CapacityOverflow ) ?;
437+ let cap = len . checked_add ( additional ) . ok_or ( CapacityOverflow ) ?;
484438 let new_layout = Layout :: array :: < T > ( cap) ;
485439
486440 // `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 ) ?;
488442 self . set_memory ( memory) ;
489443 Ok ( ( ) )
490444 }
@@ -518,7 +472,6 @@ impl<T, A: AllocRef> RawVec<T, A> {
518472// much smaller than the number of `T` types.)
519473fn finish_grow < A > (
520474 new_layout : Result < Layout , LayoutErr > ,
521- placement : ReallocPlacement ,
522475 current_memory : Option < ( NonNull < u8 > , Layout ) > ,
523476 alloc : & mut A ,
524477) -> Result < MemoryBlock , TryReserveError >
@@ -532,12 +485,9 @@ where
532485
533486 let memory = if let Some ( ( ptr, old_layout) ) = current_memory {
534487 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 ) }
536489 } else {
537- match placement {
538- MayMove => alloc. alloc ( new_layout, Uninitialized ) ,
539- InPlace => Err ( AllocErr ) ,
540- }
490+ alloc. alloc ( new_layout, Uninitialized )
541491 }
542492 . map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?;
543493
0 commit comments