@@ -190,9 +190,12 @@ impl<T, A: Allocator> RawVec<T, A> {
190
190
Err ( _) => handle_alloc_error ( layout) ,
191
191
} ;
192
192
193
+ // Allocators currently return a `NonNull<[u8]>` whose length
194
+ // matches the size requested. If that ever changes, the capacity
195
+ // here should change to `ptr.len() / mem::size_of::<T>()`.
193
196
Self {
194
197
ptr : unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ,
195
- cap : Self :: capacity_from_bytes ( ptr . len ( ) ) ,
198
+ cap : capacity ,
196
199
alloc,
197
200
}
198
201
}
@@ -337,7 +340,7 @@ impl<T, A: Allocator> RawVec<T, A> {
337
340
if self . needs_to_grow ( len, additional) { self . grow_exact ( len, additional) } else { Ok ( ( ) ) }
338
341
}
339
342
340
- /// Shrinks the allocation down to the specified amount . If the given amount
343
+ /// Shrinks the buffer down to the specified capacity . If the given amount
341
344
/// is 0, actually completely deallocates.
342
345
///
343
346
/// # Panics
@@ -348,8 +351,8 @@ impl<T, A: Allocator> RawVec<T, A> {
348
351
///
349
352
/// Aborts on OOM.
350
353
#[ cfg( not( no_global_oom_handling) ) ]
351
- pub fn shrink_to_fit ( & mut self , amount : usize ) {
352
- handle_reserve ( self . shrink ( amount ) ) ;
354
+ pub fn shrink_to_fit ( & mut self , cap : usize ) {
355
+ handle_reserve ( self . shrink ( cap ) ) ;
353
356
}
354
357
}
355
358
@@ -360,14 +363,12 @@ impl<T, A: Allocator> RawVec<T, A> {
360
363
additional > self . capacity ( ) . wrapping_sub ( len)
361
364
}
362
365
363
- fn capacity_from_bytes ( excess : usize ) -> usize {
364
- debug_assert_ne ! ( mem:: size_of:: <T >( ) , 0 ) ;
365
- excess / mem:: size_of :: < T > ( )
366
- }
367
-
368
- fn set_ptr ( & mut self , ptr : NonNull < [ u8 ] > ) {
366
+ fn set_ptr_and_cap ( & mut self , ptr : NonNull < [ u8 ] > , cap : usize ) {
367
+ // Allocators currently return a `NonNull<[u8]>` whose length matches
368
+ // the size requested. If that ever changes, the capacity here should
369
+ // change to `ptr.len() / mem::size_of::<T>()`.
369
370
self . ptr = unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ;
370
- self . cap = Self :: capacity_from_bytes ( ptr . len ( ) ) ;
371
+ self . cap = cap ;
371
372
}
372
373
373
374
// This method is usually instantiated many times. So we want it to be as
@@ -399,7 +400,7 @@ impl<T, A: Allocator> RawVec<T, A> {
399
400
400
401
// `finish_grow` is non-generic over `T`.
401
402
let ptr = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
402
- self . set_ptr ( ptr) ;
403
+ self . set_ptr_and_cap ( ptr, cap ) ;
403
404
Ok ( ( ) )
404
405
}
405
406
@@ -418,23 +419,23 @@ impl<T, A: Allocator> RawVec<T, A> {
418
419
419
420
// `finish_grow` is non-generic over `T`.
420
421
let ptr = finish_grow ( new_layout, self . current_memory ( ) , & mut self . alloc ) ?;
421
- self . set_ptr ( ptr) ;
422
+ self . set_ptr_and_cap ( ptr, cap ) ;
422
423
Ok ( ( ) )
423
424
}
424
425
425
- fn shrink ( & mut self , amount : usize ) -> Result < ( ) , TryReserveError > {
426
- assert ! ( amount <= self . capacity( ) , "Tried to shrink to a larger capacity" ) ;
426
+ fn shrink ( & mut self , cap : usize ) -> Result < ( ) , TryReserveError > {
427
+ assert ! ( cap <= self . capacity( ) , "Tried to shrink to a larger capacity" ) ;
427
428
428
429
let ( ptr, layout) = if let Some ( mem) = self . current_memory ( ) { mem } else { return Ok ( ( ) ) } ;
429
- let new_size = amount * mem:: size_of :: < T > ( ) ;
430
+ let new_size = cap * mem:: size_of :: < T > ( ) ;
430
431
431
432
let ptr = unsafe {
432
433
let new_layout = Layout :: from_size_align_unchecked ( new_size, layout. align ( ) ) ;
433
434
self . alloc
434
435
. shrink ( ptr, layout, new_layout)
435
436
. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?
436
437
} ;
437
- self . set_ptr ( ptr) ;
438
+ self . set_ptr_and_cap ( ptr, cap ) ;
438
439
Ok ( ( ) )
439
440
}
440
441
}
0 commit comments