diff options
| author | Simon Sapin <simon.sapin@exyr.org> | 2019-06-12 18:47:58 +0200 |
|---|---|---|
| committer | Simon Sapin <simon.sapin@exyr.org> | 2019-08-16 18:08:37 +0200 |
| commit | 59a340963fa5d8b5507d95cd015f7ca2855ba151 (patch) | |
| tree | 20ec73bf0c84a434d0d1192fc4216eb64e1d1161 /src/liballoc/raw_vec.rs | |
| parent | a92c29b2385f4999d4cd9ef7589f1fc07ef9cdfb (diff) | |
| download | rust-59a340963fa5d8b5507d95cd015f7ca2855ba151.tar.gz rust-59a340963fa5d8b5507d95cd015f7ca2855ba151.zip | |
Add the Layout of the failed allocation to TryReserveError::AllocError
… and add a separately-unstable field to force non-exhaustive matching (`#[non_exhaustive]` is no implemented yet on enum variants) so that we have the option to later expose the allocator’s error value. CC https://github.com/rust-lang/wg-allocators/issues/23
Diffstat (limited to 'src/liballoc/raw_vec.rs')
| -rw-r--r-- | src/liballoc/raw_vec.rs | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/src/liballoc/raw_vec.rs b/src/liballoc/raw_vec.rs index fed5a16599d..bc8a38f6b3a 100644 --- a/src/liballoc/raw_vec.rs +++ b/src/liballoc/raw_vec.rs @@ -7,7 +7,7 @@ use core::ops::Drop; use core::ptr::{self, NonNull, Unique}; use core::slice; -use crate::alloc::{Alloc, Layout, Global, handle_alloc_error}; +use crate::alloc::{Alloc, Layout, Global, AllocErr, handle_alloc_error}; use crate::collections::TryReserveError::{self, *}; use crate::boxed::Box; @@ -413,7 +413,7 @@ impl<T, A: Alloc> RawVec<T, A> { pub fn reserve_exact(&mut self, used_capacity: usize, needed_extra_capacity: usize) { match self.reserve_internal(used_capacity, needed_extra_capacity, Infallible, Exact) { Err(CapacityOverflow) => capacity_overflow(), - Err(AllocErr) => unreachable!(), + Err(AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } } } @@ -494,7 +494,7 @@ impl<T, A: Alloc> RawVec<T, A> { pub fn reserve(&mut self, used_capacity: usize, needed_extra_capacity: usize) { match self.reserve_internal(used_capacity, needed_extra_capacity, Infallible, Amortized) { Err(CapacityOverflow) => capacity_overflow(), - Err(AllocErr) => unreachable!(), + Err(AllocError { .. }) => unreachable!(), Ok(()) => { /* yay */ } } } @@ -642,8 +642,6 @@ impl<T, A: Alloc> RawVec<T, A> { strategy: ReserveStrategy, ) -> Result<(), TryReserveError> { unsafe { - use crate::alloc::AllocErr; - // NOTE: we don't early branch on ZSTs here because we want this // to actually catch "asking for more than usize::MAX" in that case. // If we make it past the first branch then we are guaranteed to @@ -672,12 +670,16 @@ impl<T, A: Alloc> RawVec<T, A> { None => self.a.alloc(new_layout), }; - match (&res, fallibility) { + let ptr = match (res, fallibility) { (Err(AllocErr), Infallible) => handle_alloc_error(new_layout), - _ => {} - } + (Err(AllocErr), Fallible) => return Err(TryReserveError::AllocError { + layout: new_layout, + non_exhaustive: (), + }), + (Ok(ptr), _) => ptr, + }; - self.ptr = res?.cast().into(); + self.ptr = ptr.cast().into(); self.cap = new_cap; Ok(()) |
