Skip to content

Commit c5975e9

Browse files
committed
Reduce duplicate in liballoc reserve error handling
1 parent 5f6bd6e commit c5975e9

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

library/alloc/src/raw_vec.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
303303
/// # }
304304
/// ```
305305
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));
311307
}
312308

313309
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -337,11 +333,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
337333
///
338334
/// Aborts on OOM.
339335
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));
345337
}
346338

347339
/// 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> {
364356
///
365357
/// Aborts on OOM.
366358
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));
372360
}
373361
}
374362

@@ -510,6 +498,16 @@ unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
510498
}
511499
}
512500

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+
513511
// We need to guarantee the following:
514512
// * We don't ever allocate `> isize::MAX` byte-size objects.
515513
// * We don't overflow `usize::MAX` and actually allocate too little.

0 commit comments

Comments
 (0)