Skip to content

Commit 0d8b30a

Browse files
committed
Remove some uses of NonNull::new_unchecked
1 parent 43e678e commit 0d8b30a

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/lib.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,13 @@ impl<T, const N: usize> RawSmallVec<T, N> {
208208
return Err(CollectionAllocErr::CapacityOverflow);
209209
}
210210

211-
if len == 0 || !was_on_heap {
211+
let new_ptr = if len == 0 || !was_on_heap {
212212
// 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
223218
} else {
224219
// use realloc
225220

@@ -234,13 +229,10 @@ impl<T, const N: usize> RawSmallVec<T, N> {
234229
// does not overflow when rounded up to alignment. since it was constructed
235230
// with Layout::array
236231
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(())
244236
}
245237
}
246238

@@ -1080,7 +1072,7 @@ impl<T, const N: usize> SmallVec<T, N> {
10801072
// so the copy is within bounds of the inline member
10811073
copy_nonoverlapping(ptr.as_ptr(), self.raw.as_mut_ptr_inline(), len);
10821074
drop(DropDealloc {
1083-
ptr: NonNull::new_unchecked(ptr.cast().as_ptr()),
1075+
ptr: ptr.cast(),
10841076
size_bytes: old_cap * size_of::<T>(),
10851077
align: align_of::<T>(),
10861078
});

0 commit comments

Comments
 (0)