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