@@ -114,7 +114,10 @@ impl<T> RawVec<T, Global> {
114
114
#[ must_use]
115
115
#[ inline]
116
116
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
+ }
118
121
}
119
122
120
123
/// Like `with_capacity`, but guarantees the buffer is zeroed.
@@ -152,7 +155,10 @@ impl<T, A: Allocator> RawVec<T, A> {
152
155
#[ cfg( not( no_global_oom_handling) ) ]
153
156
#[ inline]
154
157
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
+ }
156
162
}
157
163
158
164
/// Like `try_with_capacity`, but parameterized over the choice of
@@ -167,7 +173,10 @@ impl<T, A: Allocator> RawVec<T, A> {
167
173
#[ cfg( not( no_global_oom_handling) ) ]
168
174
#[ inline]
169
175
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
+ }
171
180
}
172
181
173
182
/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
@@ -326,7 +335,9 @@ impl<T, A: Allocator> RawVec<T, A> {
326
335
len : usize ,
327
336
additional : usize ,
328
337
) {
329
- handle_reserve ( slf. grow_amortized ( len, additional) ) ;
338
+ if let Err ( err) = slf. grow_amortized ( len, additional) {
339
+ handle_reserve_err ( err) ;
340
+ }
330
341
}
331
342
332
343
if self . needs_to_grow ( len, additional) {
@@ -339,7 +350,9 @@ impl<T, A: Allocator> RawVec<T, A> {
339
350
#[ cfg( not( no_global_oom_handling) ) ]
340
351
#[ inline( never) ]
341
352
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
+ }
343
356
}
344
357
345
358
/// The same as `reserve`, but returns on errors instead of panicking or aborting.
@@ -373,7 +386,9 @@ impl<T, A: Allocator> RawVec<T, A> {
373
386
/// Aborts on OOM.
374
387
#[ cfg( not( no_global_oom_handling) ) ]
375
388
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
+ }
377
392
}
378
393
379
394
/// 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> {
404
419
/// Aborts on OOM.
405
420
#[ cfg( not( no_global_oom_handling) ) ]
406
421
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
+ }
408
425
}
409
426
}
410
427
@@ -559,12 +576,10 @@ unsafe impl<#[may_dangle] T, A: Allocator> Drop for RawVec<T, A> {
559
576
560
577
// Central function for reserve error handling.
561
578
#[ 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) ,
568
583
}
569
584
}
570
585
0 commit comments