@@ -178,7 +178,7 @@ impl<T, A: Allocator> RawVec<T, A> {
178
178
179
179
Self {
180
180
ptr : unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ,
181
- cap : Self :: capacity_from_bytes ( ptr. len ( ) ) ,
181
+ cap : ptr. len ( ) / mem :: size_of :: < T > ( ) ,
182
182
alloc,
183
183
}
184
184
}
@@ -346,16 +346,6 @@ impl<T, A: Allocator> RawVec<T, A> {
346
346
additional > self . capacity ( ) . wrapping_sub ( len)
347
347
}
348
348
349
- fn capacity_from_bytes ( excess : usize ) -> usize {
350
- debug_assert_ne ! ( mem:: size_of:: <T >( ) , 0 ) ;
351
- excess / mem:: size_of :: < T > ( )
352
- }
353
-
354
- fn set_ptr ( & mut self , ptr : NonNull < [ u8 ] > ) {
355
- self . ptr = unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ;
356
- self . cap = Self :: capacity_from_bytes ( ptr. len ( ) ) ;
357
- }
358
-
359
349
// This method must only be called after `needs_to_grow(len, additional)`
360
350
// succeeds. Otherwise, if `T` is zero-sized it will cause a divide by
361
351
// zero.
@@ -370,15 +360,16 @@ impl<T, A: Allocator> RawVec<T, A> {
370
360
fn grow_amortized ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
371
361
// `finish_grow_amortized` is non-generic over `T`.
372
362
let elem_layout = Layout :: new :: < T > ( ) ;
373
- let ptr = finish_grow_amortized (
363
+ let ( ptr, cap ) = finish_grow_amortized (
374
364
len,
375
365
additional,
376
366
elem_layout,
377
367
self . cap ,
378
368
self . current_memory ( ) ,
379
369
& mut self . alloc ,
380
370
) ?;
381
- self . set_ptr ( ptr) ;
371
+ self . ptr = unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ;
372
+ self . cap = cap;
382
373
Ok ( ( ) )
383
374
}
384
375
@@ -392,14 +383,15 @@ impl<T, A: Allocator> RawVec<T, A> {
392
383
fn grow_exact ( & mut self , len : usize , additional : usize ) -> Result < ( ) , TryReserveError > {
393
384
// `finish_grow_exact` is non-generic over `T`.
394
385
let elem_layout = Layout :: new :: < T > ( ) ;
395
- let ptr = finish_grow_exact (
386
+ let ( ptr, cap ) = finish_grow_exact (
396
387
len,
397
388
additional,
398
389
elem_layout,
399
390
self . current_memory ( ) ,
400
391
& mut self . alloc ,
401
392
) ?;
402
- self . set_ptr ( ptr) ;
393
+ self . ptr = unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ;
394
+ self . cap = cap;
403
395
Ok ( ( ) )
404
396
}
405
397
@@ -415,7 +407,8 @@ impl<T, A: Allocator> RawVec<T, A> {
415
407
. shrink ( ptr, layout, new_layout)
416
408
. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } ) ?
417
409
} ;
418
- self . set_ptr ( ptr) ;
410
+ self . ptr = unsafe { Unique :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) } ;
411
+ self . cap = ptr. len ( ) / mem:: size_of :: < T > ( ) ;
419
412
Ok ( ( ) )
420
413
}
421
414
}
@@ -432,7 +425,7 @@ fn finish_grow_amortized<A>(
432
425
current_cap : usize ,
433
426
current_memory : Option < ( NonNull < u8 > , Layout ) > ,
434
427
alloc : & mut A ,
435
- ) -> Result < NonNull < [ u8 ] > , TryReserveError >
428
+ ) -> Result < ( NonNull < [ u8 ] > , usize ) , TryReserveError >
436
429
where
437
430
A : Allocator ,
438
431
{
@@ -467,18 +460,20 @@ where
467
460
468
461
let new_layout = unsafe { Layout :: from_size_align_unchecked ( array_size, elem_layout. align ( ) ) } ;
469
462
470
- let memory = if let Some ( ( ptr , old_layout) ) = current_memory {
463
+ let new_ptr = if let Some ( ( old_ptr , old_layout) ) = current_memory {
471
464
debug_assert_eq ! ( old_layout. align( ) , new_layout. align( ) ) ;
472
465
unsafe {
473
466
// The allocator checks for alignment equality
474
467
intrinsics:: assume ( old_layout. align ( ) == new_layout. align ( ) ) ;
475
- alloc. grow ( ptr , old_layout, new_layout)
468
+ alloc. grow ( old_ptr , old_layout, new_layout)
476
469
}
477
470
} else {
478
471
alloc. allocate ( new_layout)
479
- } ;
472
+ }
473
+ . map_err ( |_| TryReserveError :: from ( AllocError { layout : new_layout, non_exhaustive : ( ) } ) ) ?;
480
474
481
- memory. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } . into ( ) )
475
+ let new_cap = new_ptr. len ( ) / elem_layout. size ( ) ;
476
+ Ok ( ( new_ptr, new_cap) )
482
477
}
483
478
484
479
// This function is outside `RawVec` to minimize compile times. See the comment
@@ -492,7 +487,7 @@ fn finish_grow_exact<A>(
492
487
elem_layout : Layout ,
493
488
current_memory : Option < ( NonNull < u8 > , Layout ) > ,
494
489
alloc : & mut A ,
495
- ) -> Result < NonNull < [ u8 ] > , TryReserveError >
490
+ ) -> Result < ( NonNull < [ u8 ] > , usize ) , TryReserveError >
496
491
where
497
492
A : Allocator ,
498
493
{
@@ -509,18 +504,20 @@ where
509
504
510
505
let new_layout = unsafe { Layout :: from_size_align_unchecked ( array_size, elem_layout. align ( ) ) } ;
511
506
512
- let memory = if let Some ( ( ptr , old_layout) ) = current_memory {
507
+ let new_ptr = if let Some ( ( old_ptr , old_layout) ) = current_memory {
513
508
debug_assert_eq ! ( old_layout. align( ) , new_layout. align( ) ) ;
514
509
unsafe {
515
510
// The allocator checks for alignment equality
516
511
intrinsics:: assume ( old_layout. align ( ) == new_layout. align ( ) ) ;
517
- alloc. grow ( ptr , old_layout, new_layout)
512
+ alloc. grow ( old_ptr , old_layout, new_layout)
518
513
}
519
514
} else {
520
515
alloc. allocate ( new_layout)
521
- } ;
516
+ }
517
+ . map_err ( |_| TryReserveError :: from ( AllocError { layout : new_layout, non_exhaustive : ( ) } ) ) ?;
522
518
523
- memory. map_err ( |_| AllocError { layout : new_layout, non_exhaustive : ( ) } . into ( ) )
519
+ let new_cap = new_ptr. len ( ) / elem_layout. size ( ) ;
520
+ Ok ( ( new_ptr, new_cap) )
524
521
}
525
522
526
523
unsafe impl < #[ may_dangle] T , A : Allocator > Drop for RawVec < T , A > {
0 commit comments