@@ -328,7 +328,8 @@ impl<T, A: Allocator> RawVec<T, A> {
328
328
#[ inline]
329
329
#[ track_caller]
330
330
pub ( crate ) fn reserve ( & mut self , len : usize , additional : usize ) {
331
- self . inner . reserve ( len, additional, T :: LAYOUT )
331
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
332
+ unsafe { self . inner . reserve ( len, additional, T :: LAYOUT ) }
332
333
}
333
334
334
335
/// A specialized version of `self.reserve(len, 1)` which requires the
@@ -337,7 +338,8 @@ impl<T, A: Allocator> RawVec<T, A> {
337
338
#[ inline( never) ]
338
339
#[ track_caller]
339
340
pub ( crate ) fn grow_one ( & mut self ) {
340
- self . inner . grow_one ( T :: LAYOUT )
341
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
342
+ unsafe { self . inner . grow_one ( T :: LAYOUT ) }
341
343
}
342
344
343
345
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -346,7 +348,8 @@ impl<T, A: Allocator> RawVec<T, A> {
346
348
len : usize ,
347
349
additional : usize ,
348
350
) -> Result < ( ) , TryReserveError > {
349
- self . inner . try_reserve ( len, additional, T :: LAYOUT )
351
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
352
+ unsafe { self . inner . try_reserve ( len, additional, T :: LAYOUT ) }
350
353
}
351
354
352
355
/// Ensures that the buffer contains at least enough space to hold `len +
@@ -369,7 +372,8 @@ impl<T, A: Allocator> RawVec<T, A> {
369
372
#[ cfg( not( no_global_oom_handling) ) ]
370
373
#[ track_caller]
371
374
pub ( crate ) fn reserve_exact ( & mut self , len : usize , additional : usize ) {
372
- self . inner . reserve_exact ( len, additional, T :: LAYOUT )
375
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
376
+ unsafe { self . inner . reserve_exact ( len, additional, T :: LAYOUT ) }
373
377
}
374
378
375
379
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
@@ -378,7 +382,8 @@ impl<T, A: Allocator> RawVec<T, A> {
378
382
len : usize ,
379
383
additional : usize ,
380
384
) -> Result < ( ) , TryReserveError > {
381
- self . inner . try_reserve_exact ( len, additional, T :: LAYOUT )
385
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
386
+ unsafe { self . inner . try_reserve_exact ( len, additional, T :: LAYOUT ) }
382
387
}
383
388
384
389
/// Shrinks the buffer down to the specified capacity. If the given amount
@@ -395,7 +400,8 @@ impl<T, A: Allocator> RawVec<T, A> {
395
400
#[ track_caller]
396
401
#[ inline]
397
402
pub ( crate ) fn shrink_to_fit ( & mut self , cap : usize ) {
398
- self . inner . shrink_to_fit ( cap, T :: LAYOUT )
403
+ // SAFETY: All calls on self.inner pass T::LAYOUT as the elem_layout
404
+ unsafe { self . inner . shrink_to_fit ( cap, T :: LAYOUT ) }
399
405
}
400
406
}
401
407
@@ -523,7 +529,7 @@ impl<A: Allocator> RawVecInner<A> {
523
529
}
524
530
525
531
#[ inline]
526
- fn current_memory ( & self , elem_layout : Layout ) -> Option < ( NonNull < u8 > , Layout ) > {
532
+ unsafe fn current_memory ( & self , elem_layout : Layout ) -> Option < ( NonNull < u8 > , Layout ) > {
527
533
if elem_layout. size ( ) == 0 || self . cap . as_inner ( ) == 0 {
528
534
None
529
535
} else {
@@ -542,45 +548,52 @@ impl<A: Allocator> RawVecInner<A> {
542
548
#[ cfg( not( no_global_oom_handling) ) ]
543
549
#[ inline]
544
550
#[ track_caller]
545
- fn reserve ( & mut self , len : usize , additional : usize , elem_layout : Layout ) {
551
+ unsafe fn reserve ( & mut self , len : usize , additional : usize , elem_layout : Layout ) {
546
552
// Callers expect this function to be very cheap when there is already sufficient capacity.
547
553
// Therefore, we move all the resizing and error-handling logic from grow_amortized and
548
554
// handle_reserve behind a call, while making sure that this function is likely to be
549
555
// inlined as just a comparison and a call if the comparison fails.
550
556
#[ cold]
551
- fn do_reserve_and_handle < A : Allocator > (
557
+ unsafe fn do_reserve_and_handle < A : Allocator > (
552
558
slf : & mut RawVecInner < A > ,
553
559
len : usize ,
554
560
additional : usize ,
555
561
elem_layout : Layout ,
556
562
) {
557
- if let Err ( err) = slf. grow_amortized ( len, additional, elem_layout) {
563
+ // SAFETY: Precondition passed to caller
564
+ if let Err ( err) = unsafe { slf. grow_amortized ( len, additional, elem_layout) } {
558
565
handle_error ( err) ;
559
566
}
560
567
}
561
568
562
569
if self . needs_to_grow ( len, additional, elem_layout) {
563
- do_reserve_and_handle ( self , len, additional, elem_layout) ;
570
+ unsafe {
571
+ do_reserve_and_handle ( self , len, additional, elem_layout) ;
572
+ }
564
573
}
565
574
}
566
575
567
576
#[ cfg( not( no_global_oom_handling) ) ]
568
577
#[ inline]
569
578
#[ track_caller]
570
- fn grow_one ( & mut self , elem_layout : Layout ) {
571
- if let Err ( err) = self . grow_amortized ( self . cap . as_inner ( ) , 1 , elem_layout) {
579
+ unsafe fn grow_one ( & mut self , elem_layout : Layout ) {
580
+ // SAFETY: Precondition passed to caller
581
+ if let Err ( err) = unsafe { self . grow_amortized ( self . cap . as_inner ( ) , 1 , elem_layout) } {
572
582
handle_error ( err) ;
573
583
}
574
584
}
575
585
576
- fn try_reserve (
586
+ unsafe fn try_reserve (
577
587
& mut self ,
578
588
len : usize ,
579
589
additional : usize ,
580
590
elem_layout : Layout ,
581
591
) -> Result < ( ) , TryReserveError > {
582
592
if self . needs_to_grow ( len, additional, elem_layout) {
583
- self . grow_amortized ( len, additional, elem_layout) ?;
593
+ // SAFETY: Precondition passed to caller
594
+ unsafe {
595
+ self . grow_amortized ( len, additional, elem_layout) ?;
596
+ }
584
597
}
585
598
unsafe {
586
599
// Inform the optimizer that the reservation has succeeded or wasn't needed
@@ -591,20 +604,24 @@ impl<A: Allocator> RawVecInner<A> {
591
604
592
605
#[ cfg( not( no_global_oom_handling) ) ]
593
606
#[ track_caller]
594
- fn reserve_exact ( & mut self , len : usize , additional : usize , elem_layout : Layout ) {
595
- if let Err ( err) = self . try_reserve_exact ( len, additional, elem_layout) {
607
+ unsafe fn reserve_exact ( & mut self , len : usize , additional : usize , elem_layout : Layout ) {
608
+ // SAFETY: Precondition passed to caller
609
+ if let Err ( err) = unsafe { self . try_reserve_exact ( len, additional, elem_layout) } {
596
610
handle_error ( err) ;
597
611
}
598
612
}
599
613
600
- fn try_reserve_exact (
614
+ unsafe fn try_reserve_exact (
601
615
& mut self ,
602
616
len : usize ,
603
617
additional : usize ,
604
618
elem_layout : Layout ,
605
619
) -> Result < ( ) , TryReserveError > {
606
620
if self . needs_to_grow ( len, additional, elem_layout) {
607
- self . grow_exact ( len, additional, elem_layout) ?;
621
+ // SAFETY: Precondition passed to caller
622
+ unsafe {
623
+ self . grow_exact ( len, additional, elem_layout) ?;
624
+ }
608
625
}
609
626
unsafe {
610
627
// Inform the optimizer that the reservation has succeeded or wasn't needed
@@ -616,8 +633,8 @@ impl<A: Allocator> RawVecInner<A> {
616
633
#[ cfg( not( no_global_oom_handling) ) ]
617
634
#[ inline]
618
635
#[ track_caller]
619
- fn shrink_to_fit ( & mut self , cap : usize , elem_layout : Layout ) {
620
- if let Err ( err) = self . shrink ( cap, elem_layout) {
636
+ unsafe fn shrink_to_fit ( & mut self , cap : usize , elem_layout : Layout ) {
637
+ if let Err ( err) = unsafe { self . shrink ( cap, elem_layout) } {
621
638
handle_error ( err) ;
622
639
}
623
640
}
@@ -636,7 +653,7 @@ impl<A: Allocator> RawVecInner<A> {
636
653
self . cap = unsafe { Cap :: new_unchecked ( cap) } ;
637
654
}
638
655
639
- fn grow_amortized (
656
+ unsafe fn grow_amortized (
640
657
& mut self ,
641
658
len : usize ,
642
659
additional : usize ,
@@ -661,14 +678,16 @@ impl<A: Allocator> RawVecInner<A> {
661
678
662
679
let new_layout = layout_array ( cap, elem_layout) ?;
663
680
664
- let ptr = finish_grow ( new_layout, self . current_memory ( elem_layout) , & mut self . alloc ) ?;
681
+ // SAFETY: Precondition passed to caller
682
+ let ptr =
683
+ finish_grow ( new_layout, unsafe { self . current_memory ( elem_layout) } , & mut self . alloc ) ?;
665
684
// SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than `isize::MAX` items
666
685
667
686
unsafe { self . set_ptr_and_cap ( ptr, cap) } ;
668
687
Ok ( ( ) )
669
688
}
670
689
671
- fn grow_exact (
690
+ unsafe fn grow_exact (
672
691
& mut self ,
673
692
len : usize ,
674
693
additional : usize ,
@@ -683,7 +702,9 @@ impl<A: Allocator> RawVecInner<A> {
683
702
let cap = len. checked_add ( additional) . ok_or ( CapacityOverflow ) ?;
684
703
let new_layout = layout_array ( cap, elem_layout) ?;
685
704
686
- let ptr = finish_grow ( new_layout, self . current_memory ( elem_layout) , & mut self . alloc ) ?;
705
+ // SAFETY: Precondition passed to caller
706
+ let ptr =
707
+ finish_grow ( new_layout, unsafe { self . current_memory ( elem_layout) } , & mut self . alloc ) ?;
687
708
// SAFETY: finish_grow would have resulted in a capacity overflow if we tried to allocate more than `isize::MAX` items
688
709
unsafe {
689
710
self . set_ptr_and_cap ( ptr, cap) ;
@@ -693,7 +714,7 @@ impl<A: Allocator> RawVecInner<A> {
693
714
694
715
#[ cfg( not( no_global_oom_handling) ) ]
695
716
#[ inline]
696
- fn shrink ( & mut self , cap : usize , elem_layout : Layout ) -> Result < ( ) , TryReserveError > {
717
+ unsafe fn shrink ( & mut self , cap : usize , elem_layout : Layout ) -> Result < ( ) , TryReserveError > {
697
718
assert ! ( cap <= self . capacity( elem_layout. size( ) ) , "Tried to shrink to a larger capacity" ) ;
698
719
// SAFETY: Just checked this isn't trying to grow
699
720
unsafe { self . shrink_unchecked ( cap, elem_layout) }
@@ -715,8 +736,12 @@ impl<A: Allocator> RawVecInner<A> {
715
736
cap : usize ,
716
737
elem_layout : Layout ,
717
738
) -> Result < ( ) , TryReserveError > {
718
- let ( ptr, layout) =
719
- if let Some ( mem) = self . current_memory ( elem_layout) { mem } else { return Ok ( ( ) ) } ;
739
+ // SAFETY: Precondition passed to caller
740
+ let ( ptr, layout) = if let Some ( mem) = unsafe { self . current_memory ( elem_layout) } {
741
+ mem
742
+ } else {
743
+ return Ok ( ( ) ) ;
744
+ } ;
720
745
721
746
// If shrinking to 0, deallocate the buffer. We don't reach this point
722
747
// for the T::IS_ZST case since current_memory() will have returned
@@ -752,7 +777,8 @@ impl<A: Allocator> RawVecInner<A> {
752
777
/// Ideally this function would take `self` by move, but it cannot because it exists to be
753
778
/// called from a `Drop` impl.
754
779
unsafe fn deallocate ( & mut self , elem_layout : Layout ) {
755
- if let Some ( ( ptr, layout) ) = self . current_memory ( elem_layout) {
780
+ // SAFETY: Precondition passed to caller
781
+ if let Some ( ( ptr, layout) ) = unsafe { self . current_memory ( elem_layout) } {
756
782
unsafe {
757
783
self . alloc . deallocate ( ptr, layout) ;
758
784
}
0 commit comments