@@ -208,18 +208,13 @@ impl<T, const N: usize> RawSmallVec<T, N> {
208
208
return Err ( CollectionAllocErr :: CapacityOverflow ) ;
209
209
}
210
210
211
- if len == 0 || !was_on_heap {
211
+ let new_ptr = if len == 0 || !was_on_heap {
212
212
// get a fresh allocation
213
-
214
- // layout has non zero size
215
- let new_ptr = alloc ( new_layout) as * mut T ;
216
- if new_ptr. is_null ( ) {
217
- Err ( CollectionAllocErr :: AllocErr { layout : new_layout } )
218
- } else {
219
- copy_nonoverlapping ( ptr, new_ptr, len) ;
220
- * self = Self :: new_heap ( NonNull :: new_unchecked ( new_ptr) , new_capacity) ;
221
- Ok ( ( ) )
222
- }
213
+ let new_ptr = alloc ( new_layout) as * mut T ; // `new_layout` has nonzero size.
214
+ let new_ptr =
215
+ NonNull :: new ( new_ptr) . ok_or ( CollectionAllocErr :: AllocErr { layout : new_layout } ) ?;
216
+ copy_nonoverlapping ( ptr, new_ptr. as_ptr ( ) , len) ;
217
+ new_ptr
223
218
} else {
224
219
// use realloc
225
220
@@ -234,13 +229,10 @@ impl<T, const N: usize> RawSmallVec<T, N> {
234
229
// does not overflow when rounded up to alignment. since it was constructed
235
230
// with Layout::array
236
231
let new_ptr = realloc ( ptr as * mut u8 , old_layout, new_layout. size ( ) ) as * mut T ;
237
- if new_ptr. is_null ( ) {
238
- Err ( CollectionAllocErr :: AllocErr { layout : new_layout } )
239
- } else {
240
- * self = Self :: new_heap ( NonNull :: new_unchecked ( new_ptr) , new_capacity) ;
241
- Ok ( ( ) )
242
- }
243
- }
232
+ NonNull :: new ( new_ptr) . ok_or ( CollectionAllocErr :: AllocErr { layout : new_layout } ) ?
233
+ } ;
234
+ * self = Self :: new_heap ( new_ptr, new_capacity) ;
235
+ Ok ( ( ) )
244
236
}
245
237
}
246
238
@@ -1080,7 +1072,7 @@ impl<T, const N: usize> SmallVec<T, N> {
1080
1072
// so the copy is within bounds of the inline member
1081
1073
copy_nonoverlapping ( ptr. as_ptr ( ) , self . raw . as_mut_ptr_inline ( ) , len) ;
1082
1074
drop ( DropDealloc {
1083
- ptr : NonNull :: new_unchecked ( ptr. cast ( ) . as_ptr ( ) ) ,
1075
+ ptr : ptr. cast ( ) ,
1084
1076
size_bytes : old_cap * size_of :: < T > ( ) ,
1085
1077
align : align_of :: < T > ( ) ,
1086
1078
} ) ;
0 commit comments