Skip to content

Commit 11f7bfa

Browse files
Rollup merge of #72734 - pickfire:liballoc, r=KodrAus
Reduce duplicate in liballoc reserve error handling Not sure if it affects compilation time.
2 parents 4519845 + c5975e9 commit 11f7bfa

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
@@ -306,11 +306,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
306306
/// # }
307307
/// ```
308308
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));
314310
}
315311

316312
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -340,11 +336,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
340336
///
341337
/// Aborts on OOM.
342338
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));
348340
}
349341

350342
/// 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> {
367359
///
368360
/// Aborts on OOM.
369361
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));
375363
}
376364
}
377365

@@ -517,6 +505,16 @@ unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> {
517505
}
518506
}
519507

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

0 commit comments

Comments
 (0)