diff options
| author | ecstatic-morse <ecstaticmorse@gmail.com> | 2020-09-21 20:40:37 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-09-21 20:40:37 -0700 |
| commit | 11f7bfab91c4a285ca5a54609322fd7b85dfc3bd (patch) | |
| tree | 449be10bd5fb5653459929e1aa4c159cd8a455b9 | |
| parent | 45198456be60a6906d24abdc3c67a31b9206188e (diff) | |
| parent | c5975e9b6c5781b3b7300b7921c14b060086e1c1 (diff) | |
| download | rust-11f7bfab91c4a285ca5a54609322fd7b85dfc3bd.tar.gz rust-11f7bfab91c4a285ca5a54609322fd7b85dfc3bd.zip | |
Rollup merge of #72734 - pickfire:liballoc, r=KodrAus
Reduce duplicate in liballoc reserve error handling Not sure if it affects compilation time.
| -rw-r--r-- | library/alloc/src/raw_vec.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs index 62675665f03..e33dddc4f98 100644 --- a/library/alloc/src/raw_vec.rs +++ b/library/alloc/src/raw_vec.rs @@ -306,11 +306,7 @@ impl<T, A: AllocRef> RawVec<T, A> { /// # } /// ``` pub fn reserve(&mut self, len: usize, additional: usize) { - match self.try_reserve(len, additional) { - Err(CapacityOverflow) => capacity_overflow(), - Err(AllocError { layout, .. }) => handle_alloc_error(layout), - Ok(()) => { /* yay */ } - } + handle_reserve(self.try_reserve(len, additional)); } /// The same as `reserve`, but returns on errors instead of panicking or aborting. @@ -340,11 +336,7 @@ impl<T, A: AllocRef> RawVec<T, A> { /// /// Aborts on OOM. pub fn reserve_exact(&mut self, len: usize, additional: usize) { - match self.try_reserve_exact(len, additional) { - Err(CapacityOverflow) => capacity_overflow(), - Err(AllocError { layout, .. }) => handle_alloc_error(layout), - Ok(()) => { /* yay */ } - } + handle_reserve(self.try_reserve_exact(len, additional)); } /// 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> { /// /// Aborts on OOM. pub fn shrink_to_fit(&mut self, amount: usize) { - match self.shrink(amount) { - Err(CapacityOverflow) => capacity_overflow(), - Err(AllocError { layout, .. }) => handle_alloc_error(layout), - Ok(()) => { /* yay */ } - } + handle_reserve(self.shrink(amount)); } } @@ -517,6 +505,16 @@ unsafe impl<#[may_dangle] T, A: AllocRef> Drop for RawVec<T, A> { } } +// Central function for reserve error handling. +#[inline] +fn handle_reserve(result: Result<(), TryReserveError>) { + match result { + Err(CapacityOverflow) => capacity_overflow(), + Err(AllocError { layout, .. }) => handle_alloc_error(layout), + Ok(()) => { /* yay */ } + } +} + // We need to guarantee the following: // * We don't ever allocate `> isize::MAX` byte-size objects. // * We don't overflow `usize::MAX` and actually allocate too little. |
