Skip to content

Commit 5af3381

Browse files
committed
Auto merge of rust-lang#122396 - kornelski:vec-err-debloat, r=<try>
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
2 parents 7de1a1f + 870d332 commit 5af3381

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

library/alloc/src/raw_vec.rs

+28-13
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ impl<T> RawVec<T, Global> {
114114
#[must_use]
115115
#[inline]
116116
pub fn with_capacity(capacity: usize) -> Self {
117-
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global))
117+
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, Global) {
118+
Ok(res) => res,
119+
Err(err) => handle_reserve_err(err),
120+
}
118121
}
119122

120123
/// Like `with_capacity`, but guarantees the buffer is zeroed.
@@ -152,7 +155,10 @@ impl<T, A: Allocator> RawVec<T, A> {
152155
#[cfg(not(no_global_oom_handling))]
153156
#[inline]
154157
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
155-
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc))
158+
match Self::try_allocate_in(capacity, AllocInit::Uninitialized, alloc) {
159+
Ok(res) => res,
160+
Err(err) => handle_reserve_err(err),
161+
}
156162
}
157163

158164
/// Like `try_with_capacity`, but parameterized over the choice of
@@ -167,7 +173,10 @@ impl<T, A: Allocator> RawVec<T, A> {
167173
#[cfg(not(no_global_oom_handling))]
168174
#[inline]
169175
pub fn with_capacity_zeroed_in(capacity: usize, alloc: A) -> Self {
170-
handle_reserve(Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc))
176+
match Self::try_allocate_in(capacity, AllocInit::Zeroed, alloc) {
177+
Ok(res) => res,
178+
Err(err) => handle_reserve_err(err),
179+
}
171180
}
172181

173182
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
@@ -326,7 +335,9 @@ impl<T, A: Allocator> RawVec<T, A> {
326335
len: usize,
327336
additional: usize,
328337
) {
329-
handle_reserve(slf.grow_amortized(len, additional));
338+
if let Err(err) = slf.grow_amortized(len, additional) {
339+
handle_reserve_err(err);
340+
}
330341
}
331342

332343
if self.needs_to_grow(len, additional) {
@@ -339,7 +350,9 @@ impl<T, A: Allocator> RawVec<T, A> {
339350
#[cfg(not(no_global_oom_handling))]
340351
#[inline(never)]
341352
pub fn reserve_for_push(&mut self, len: usize) {
342-
handle_reserve(self.grow_amortized(len, 1));
353+
if let Err(err) = self.grow_amortized(len, 1) {
354+
handle_reserve_err(err);
355+
}
343356
}
344357

345358
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -373,7 +386,9 @@ impl<T, A: Allocator> RawVec<T, A> {
373386
/// Aborts on OOM.
374387
#[cfg(not(no_global_oom_handling))]
375388
pub fn reserve_exact(&mut self, len: usize, additional: usize) {
376-
handle_reserve(self.try_reserve_exact(len, additional));
389+
if let Err(err) = self.try_reserve_exact(len, additional) {
390+
handle_reserve_err(err);
391+
}
377392
}
378393

379394
/// The same as `reserve_exact`, but returns on errors instead of panicking or aborting.
@@ -404,7 +419,9 @@ impl<T, A: Allocator> RawVec<T, A> {
404419
/// Aborts on OOM.
405420
#[cfg(not(no_global_oom_handling))]
406421
pub fn shrink_to_fit(&mut self, cap: usize) {
407-
handle_reserve(self.shrink(cap));
422+
if let Err(err) = self.shrink(cap) {
423+
handle_reserve_err(err);
424+
}
408425
}
409426
}
410427

@@ -559,12 +576,10 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
559576

560577
// Central function for reserve error handling.
561578
#[cfg(not(no_global_oom_handling))]
562-
#[inline]
563-
fn handle_reserve<T>(result: Result<T, TryReserveError>) -> T {
564-
match result.map_err(|e| e.kind()) {
565-
Ok(res) => res,
566-
Err(CapacityOverflow) => capacity_overflow(),
567-
Err(AllocError { layout, .. }) => handle_alloc_error(layout),
579+
fn handle_reserve_err(e: TryReserveError) -> ! {
580+
match e.kind() {
581+
CapacityOverflow => capacity_overflow(),
582+
AllocError { layout, .. } => handle_alloc_error(layout),
568583
}
569584
}
570585

library/alloc/src/vec/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl<T> Vec<T> {
478478
#[stable(feature = "rust1", since = "1.0.0")]
479479
#[must_use]
480480
pub fn with_capacity(capacity: usize) -> Self {
481-
Self::with_capacity_in(capacity, Global)
481+
Vec { buf: RawVec::with_capacity_in(capacity, Global), len: 0 }
482482
}
483483

484484
/// Constructs a new, empty `Vec<T>` with at least the specified capacity.

0 commit comments

Comments
 (0)