@@ -85,8 +85,8 @@ impl<T, A: Alloc> RawVec<T, A> {
85
85
unsafe {
86
86
let elem_size = mem:: size_of :: < T > ( ) ;
87
87
88
- let alloc_size = cap. checked_mul ( elem_size) . expect ( "capacity overflow" ) ;
89
- alloc_guard ( alloc_size) . expect ( "capacity overflow" ) ;
88
+ let alloc_size = cap. checked_mul ( elem_size) . unwrap_or_else ( || capacity_overflow ( ) ) ;
89
+ alloc_guard ( alloc_size) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
90
90
91
91
// handles ZSTs and `cap = 0` alike
92
92
let ptr = if alloc_size == 0 {
@@ -309,7 +309,7 @@ impl<T, A: Alloc> RawVec<T, A> {
309
309
// `from_size_align_unchecked`.
310
310
let new_cap = 2 * self . cap ;
311
311
let new_size = new_cap * elem_size;
312
- alloc_guard ( new_size) . expect ( "capacity overflow" ) ;
312
+ alloc_guard ( new_size) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
313
313
let ptr_res = self . a . realloc ( NonNull :: from ( self . ptr ) . as_opaque ( ) ,
314
314
cur,
315
315
new_size) ;
@@ -368,7 +368,7 @@ impl<T, A: Alloc> RawVec<T, A> {
368
368
// overflow and the alignment is sufficiently small.
369
369
let new_cap = 2 * self . cap ;
370
370
let new_size = new_cap * elem_size;
371
- alloc_guard ( new_size) . expect ( "capacity overflow" ) ;
371
+ alloc_guard ( new_size) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
372
372
match self . a . grow_in_place ( NonNull :: from ( self . ptr ) . as_opaque ( ) , old_layout, new_size) {
373
373
Ok ( _) => {
374
374
// We can't directly divide `size`.
@@ -440,7 +440,7 @@ impl<T, A: Alloc> RawVec<T, A> {
440
440
441
441
pub fn reserve_exact ( & mut self , used_cap : usize , needed_extra_cap : usize ) {
442
442
match self . try_reserve_exact ( used_cap, needed_extra_cap) {
443
- Err ( CapacityOverflow ) => panic ! ( "capacity overflow" ) ,
443
+ Err ( CapacityOverflow ) => capacity_overflow ( ) ,
444
444
Err ( AllocErr ) => self . a . oom ( ) ,
445
445
Ok ( ( ) ) => { /* yay */ }
446
446
}
@@ -550,7 +550,7 @@ impl<T, A: Alloc> RawVec<T, A> {
550
550
/// The same as try_reserve, but errors are lowered to a call to oom().
551
551
pub fn reserve ( & mut self , used_cap : usize , needed_extra_cap : usize ) {
552
552
match self . try_reserve ( used_cap, needed_extra_cap) {
553
- Err ( CapacityOverflow ) => panic ! ( "capacity overflow" ) ,
553
+ Err ( CapacityOverflow ) => capacity_overflow ( ) ,
554
554
Err ( AllocErr ) => self . a . oom ( ) ,
555
555
Ok ( ( ) ) => { /* yay */ }
556
556
}
@@ -591,15 +591,15 @@ impl<T, A: Alloc> RawVec<T, A> {
591
591
}
592
592
593
593
let new_cap = self . amortized_new_size ( used_cap, needed_extra_cap)
594
- . expect ( "capacity overflow" ) ;
594
+ . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
595
595
596
596
// Here, `cap < used_cap + needed_extra_cap <= new_cap`
597
597
// (regardless of whether `self.cap - used_cap` wrapped).
598
598
// Therefore we can safely call grow_in_place.
599
599
600
600
let new_layout = Layout :: new :: < T > ( ) . repeat ( new_cap) . unwrap ( ) . 0 ;
601
601
// FIXME: may crash and burn on over-reserve
602
- alloc_guard ( new_layout. size ( ) ) . expect ( "capacity overflow" ) ;
602
+ alloc_guard ( new_layout. size ( ) ) . unwrap_or_else ( |_| capacity_overflow ( ) ) ;
603
603
match self . a . grow_in_place (
604
604
NonNull :: from ( self . ptr ) . as_opaque ( ) , old_layout, new_layout. size ( ) ,
605
605
) {
@@ -731,6 +731,13 @@ fn alloc_guard(alloc_size: usize) -> Result<(), CollectionAllocErr> {
731
731
}
732
732
}
733
733
734
+ // One central function responsible for reporting capacity overflows. This'll
735
+ // ensure that the code generation related to these panics is minimal as there's
736
+ // only one location which panics rather than a bunch throughout the module.
737
+ fn capacity_overflow ( ) -> ! {
738
+ panic ! ( "capacity overflow" )
739
+ }
740
+
734
741
#[ cfg( test) ]
735
742
mod tests {
736
743
use super :: * ;
0 commit comments