Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9f8f34

Browse files
committed
Auto merge of rust-lang#122396 - kornelski:vec-err-debloat, r=joboet
Less generic code for Vec allocations Follow up to rust-lang#120504 (comment) which hopefully makes compilation faster.
2 parents 0157da4 + 443e29c commit c9f8f34

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

‎library/alloc/src/raw_vec.rs‎

Lines changed: 29 additions & 13 deletions
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_error(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_error(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_error(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_error(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_error(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_error(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_error(err);
424+
}
408425
}
409426
}
410427

@@ -559,12 +576,11 @@ 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+
#[cold]
580+
fn handle_error(e: TryReserveError) -> ! {
581+
match e.kind() {
582+
CapacityOverflow => capacity_overflow(),
583+
AllocError { layout, .. } => handle_alloc_error(layout),
568584
}
569585
}
570586

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /