@@ -306,11 +306,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
306
306
/// # }
307
307
/// ```
308
308
pub fn reserve ( & mut self , len : usize , additional : usize ) {
309
- match self . try_reserve ( len, additional) {
310
- Err ( CapacityOverflow ) => capacity_overflow ( ) ,
311
- Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
312
- Ok ( ( ) ) => { /* yay */ }
313
- }
309
+ handle_reserve ( self . try_reserve ( len, additional) ) ;
314
310
}
315
311
316
312
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -340,11 +336,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
340
336
///
341
337
/// Aborts on OOM.
342
338
pub fn reserve_exact ( & mut self , len : usize , additional : usize ) {
343
- match self . try_reserve_exact ( len, additional) {
344
- Err ( CapacityOverflow ) => capacity_overflow ( ) ,
345
- Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
346
- Ok ( ( ) ) => { /* yay */ }
347
- }
339
+ handle_reserve ( self . try_reserve_exact ( len, additional) ) ;
348
340
}
349
341
350
342
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
@@ -367,11 +359,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
367
359
///
368
360
/// Aborts on OOM.
369
361
pub fn shrink_to_fit ( & mut self , amount : usize ) {
370
- match self . shrink ( amount) {
371
- Err ( CapacityOverflow ) => capacity_overflow ( ) ,
372
- Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
373
- Ok ( ( ) ) => { /* yay */ }
374
- }
362
+ handle_reserve ( self . shrink ( amount) ) ;
375
363
}
376
364
}
377
365
@@ -517,6 +505,16 @@ unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
517
505
}
518
506
}
519
507
508
+ // Central function for reserve error handling.
509
+ #[ inline]
510
+ fn handle_reserve ( result : Result < ( ) , TryReserveError > ) {
511
+ match result {
512
+ Err ( CapacityOverflow ) => capacity_overflow ( ) ,
513
+ Err ( AllocError { layout, .. } ) => handle_alloc_error ( layout) ,
514
+ Ok ( ( ) ) => { /* yay */ }
515
+ }
516
+ }
517
+
520
518
// We need to guarantee the following:
521
519
// * We don't ever allocate `> isize::MAX` byte-size objects.
522
520
// * We don't overflow `usize::MAX` and actually allocate too little.
0 commit comments