Auto merge of #122396 - kornelski:vec-err-debloat, r=joboet

Less generic code for Vec allocations

Follow up to https://github.com/rust-lang/rust/pull/120504#issuecomment-1989826191 which hopefully makes compilation faster.
This commit is contained in:
bors 2024-03-27 19:58:44 +00:00
commit c9f8f3438a

View File

@ -114,7 +114,10 @@ pub const fn new() -> Self {
#[must_use] #[must_use]
#[inline] #[inline]
pub fn with_capacity(capacity: usize) -> Self { pub fn with_capacity(capacity: usize) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global)) match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global) {
Ok(res) => res,
Err(err) => handle_error(err),
}
} }
/// Like `with_capacity`, but guarantees the buffer is zeroed. /// Like `with_capacity`, but guarantees the buffer is zeroed.
@ -152,7 +155,10 @@ pub const fn new_in(alloc: A) -> Self {
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline] #[inline]
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc)) match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) {
Ok(res) => res,
Err(err) => handle_error(err),
}
} }
/// Like `try_with_capacity`, but parameterized over the choice of /// Like `try_with_capacity`, but parameterized over the choice of
@ -167,7 +173,10 @@ pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserv
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline] #[inline]
pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self { pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc)) match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) {
Ok(res) => res,
Err(err) => handle_error(err),
}
} }
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`. /// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
@ -326,7 +335,9 @@ fn do_reserve_and_handle<T, A: Allocator>(
len: usize, len: usize,
additional: usize, additional: usize,
) { ) {
handle_reserve(slf.grow_amortized(len, additional)); if let Err(err) = slf.grow_amortized(len, additional) {
handle_error(err);
}
} }
if self.needs_to_grow(len, additional) { if self.needs_to_grow(len, additional) {
@ -339,7 +350,9 @@ fn do_reserve_and_handle<T, A: Allocator>(
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline(never)] #[inline(never)]
pub fn reserve_for_push(&mut self, len: usize) { pub fn reserve_for_push(&mut self, len: usize) {
handle_reserve(self.grow_amortized(len, 1)); if let Err(err) = self.grow_amortized(len, 1) {
handle_error(err);
}
} }
/// The same as `reserve`, but returns on errors instead of panicking or aborting. /// The same as `reserve`, but returns on errors instead of panicking or aborting.
@ -373,7 +386,9 @@ pub fn try_reserve(&mut self, len: usize, additional: usize) -> Result<(), TryRe
/// Aborts on OOM. /// Aborts on OOM.
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
pub fn reserve_exact(&mut self, len: usize, additional: usize) { pub fn reserve_exact(&mut self, len: usize, additional: usize) {
handle_reserve(self.try_reserve_exact(len, additional)); if let Err(err) = self.try_reserve_exact(len, additional) {
handle_error(err);
}
} }
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting. /// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
@ -404,7 +419,9 @@ pub fn try_reserve_exact(
/// Aborts on OOM. /// Aborts on OOM.
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
pub fn shrink_to_fit(&mut self, cap: usize) { pub fn shrink_to_fit(&mut self, cap: usize) {
handle_reserve(self.shrink(cap)); if let Err(err) = self.shrink(cap) {
handle_error(err);
}
} }
} }
@ -559,12 +576,11 @@ fn drop(&mut self) {
// Central function for reserve error handling. // Central function for reserve error handling.
#[cfg(not(no_global_oom_handling))] #[cfg(not(no_global_oom_handling))]
#[inline] #[cold]
fn handle_reserve<T>(result: Result<T, TryReserveError>) -> T { fn handle_error(e: TryReserveError) -> ! {
match result.map_err(|e| e.kind()) { match e.kind() {
Ok(res) => res, CapacityOverflow => capacity_overflow(),
Err(CapacityOverflow) => capacity_overflow(), AllocError { layout, .. } => handle_alloc_error(layout),
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
} }
} }